By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,293 Members | 1,621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,293 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Here's some code that I use to chop up a bitmap containing 12 each 32x32
bitmaps within one bitmap sized 32 high x 384 long:

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

"Mad Scientist Jr" wrote:
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

Nov 21 '05 #2

P: n/a
Thanks a bunch - this worked like a charm !

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.