all groups > dotnet drawing api > january 2005 >
You're in the

dotnet drawing api

group:

copy a portion of a bitmap to another bitmap (via picturebox or other method) ?


copy a portion of a bitmap to another bitmap (via picturebox or other method) ? Mad Scientist Jr
1/28/2005 6:45:10 AM
dotnet drawing api: I have a bitmap (32 pixels high, 8192 pixels wide) that contains 255
images, each 32 pixels wide, that I would like to chop up into
individual 32x32 bitmap files. Rather than spending hours in Paint or
Photoshop I would like to do this programmatically. My code below
attempts to load in the original bitmap, crop it at the desired
location, and save it to the correct file. I don't think I'm doing this
right, I tried messing with different picturebox properties/methods
such as .SetBounds. I would either like to crop the full bitmap and
save it, or copy the portion to a 2nd 32x32 bitmap and save it. Any
help appreciated....

sub BreakAllTiles()
dim iLoop as int16
for iLoop = 0 to 255
call TileBreaker(iLoop)
next
end sub

Sub TileBreaker(ByVal iImageNum As Int16)
Dim iX As Int16
Dim iY As Int16
Dim pb1 As New PictureBox
Dim pb2 As New PictureBox
Dim sFile As String
Dim sNumber As String
pb1.Size = New Size(8192, 32) ' holds the bitmap with all the images
pb2.Size = New Size(32, 32) ' holds the desired 32x32 square
sFile = "C:\temp\Images\All255Images.bmp"
pb1.Image = pb1.Image.FromFile(sFile) ' get the full image
iX = 0 + (iImageNum * 32) ' calculate x position of the desired square
iY = 0 ' y position is always zero in this case
pb1.SetBounds(iX, iY, 32, 32) ' select the desired square - here we
would like to crop pb1 at position (iX,iY) to size 32,32
'OR copy a 32x32 square of pb1 at (iX,iY), to pb2, and pb2.image.save
sNumber = "000" & iImageNum.ToString
sFile = "C:\temp\images\image" & sNumber.Substring(sNumber.Length - 3,
3) & ".bmp"
pb1.Image.Save(sFile, System.Drawing.Imaging.ImageFormat.Bmp) ' save
the cropped image
end sub ' TileBreaker
Re: copy a portion of a bitmap to another bitmap (via picturebox or other method) ? Bob Powell [MVP]
1/28/2005 7:28:45 PM
PictureBox is a control for displaying pictures. Not a magic thing for
manipulating them with.

The GDI+ FAQ has information on how to draw an image and the Beginners Guide
to GDI+ has an article on how to use images.

Throw out the picturebox nonsense and use bitmaps and Graphics objects.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]

Re: copy a portion of a bitmap to another bitmap (via picturebox or other method) ? Mad Scientist Jr
1/31/2005 6:27:41 AM
Thanks but the answer I was looking for was:

From: Dennis <Den...@discussions.microsoft.com>
Date: Fri, 28 Jan 2005 17:39:04 -0800
Local: Fri, Jan 28 2005 5:39 pm
Subject: RE: copy a portion of a bitmap to another bitmap (via
picturebox or ot

Here's some code that I use to chop up a bitmap containing 1=AD2 each
32x32
bitmaps within one bitmap sized 32 high x 384 long:

dim v_Img32 as ImageList =3D New ImageList
Dim bm As New Bitmap("C:\Images.bmp")
'Get 32x32 Images
dim format as pixelformat =3D bm.PixelFormat
v_Img32.ImageSize =3D New Size(32, 32)
v_Img32.TransparentColor =3D bm.GetPixel(0, 0) 'makes the orig=ADin pixel
transparent
For i =3D 0 To 11
cloneRect =3D New RectangleF(i * 32, 0, 32, 32)
v_Img32.Images.Add(bm.Clone(cloneRect, format))
Next=20
bm.Dispose()
AddThis Social Bookmark Button