Creating a subset of a Bitmap

  • Thread starter Thread starter Peter Oliphant
  • Start date Start date
P

Peter Oliphant

I want to create a new Bitmap which is a portion of an existing Bitmap. For
example, if I have a Bitmap that is 100x100 in size I might want to create a
new Bitmap that is equivalent to the one located at (x,y)=(10,20)
(upper-left hand corner) and is 50x50 in size of the source Bitmap. A
sub-Bitmap if you will, sort of the equivalent to sub-strings for a source
string.

I don't think this works, but something like:

Bitmap^ src_bm = gcnew Bitmap( "bm100x100.bmp" ) ;
Bitmap^ sub_bm = gcnew Bitmap( src_bm(Rectangle(10,20,50,50)) ) ;

Now I can do this via SLOW code ala creating the sub-bitmap of the proper
size and then copying pixels one-by-one from the source bitmap via GetPixel
and SetPixel. But I'm hoping that the construct for a sub-bitmap exists, as
it is quite natural. I can easily DRAW a sub-bitmap with the Graphics class,
but I want to create it as a new Bitmap.

Should I use the pixel-by-pixel method, or is there a faster and/or easier
way?

Thanx in advance for response!

[==Peter==]

PS - I'm developing in MS VC++ 2005 Express ala /cli (managed) code. This is
the Bitmap class I'm speaking of (since there may be more than one with the
same name but different namespaces):

ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/cpref8/html/T_System_Drawing_Image_Members.htm
 
1 create an empby bitmap
2 use Graphics::FromImage to create a Graphics from the empby bitmap
3 draw the desired potion of your bitmap to the Graphics
4 save the bitmap you created at step 1 and altered in step 3
 
Thanks, Sheng! I tried it and it works great!

Not only that, this opens up a lot more possibilities I hadn't considered,
since I didn't know before how to get a Graphics object from an Image
before! Thanks again!

[==Peter==]

Sheng Jiang said:
1 create an empby bitmap
2 use Graphics::FromImage to create a Graphics from the empby bitmap
3 draw the desired potion of your bitmap to the Graphics
4 save the bitmap you created at step 1 and altered in step 3
--
Sheng Jiang
Microsoft MVP in VC++
Peter Oliphant said:
I want to create a new Bitmap which is a portion of an existing Bitmap. For
example, if I have a Bitmap that is 100x100 in size I might want to
create a
new Bitmap that is equivalent to the one located at (x,y)=(10,20)
(upper-left hand corner) and is 50x50 in size of the source Bitmap. A
sub-Bitmap if you will, sort of the equivalent to sub-strings for a
source
string.

I don't think this works, but something like:

Bitmap^ src_bm = gcnew Bitmap( "bm100x100.bmp" ) ;
Bitmap^ sub_bm = gcnew Bitmap( src_bm(Rectangle(10,20,50,50)) ) ;

Now I can do this via SLOW code ala creating the sub-bitmap of the proper
size and then copying pixels one-by-one from the source bitmap via GetPixel
and SetPixel. But I'm hoping that the construct for a sub-bitmap exists, as
it is quite natural. I can easily DRAW a sub-bitmap with the Graphics class,
but I want to create it as a new Bitmap.

Should I use the pixel-by-pixel method, or is there a faster and/or
easier
way?

Thanx in advance for response!

[==Peter==]

PS - I'm developing in MS VC++ 2005 Express ala /cli (managed) code. This is
the Bitmap class I'm speaking of (since there may be more than one with the
same name but different namespaces):

ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/cpref8/html/T_System_Dra
wing_Image_Members.htm
 
Back
Top