PC Review


Reply
Thread Tools Rate Thread

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

 
 
Mad Scientist Jr
Guest
Posts: n/a
 
      28th Jan 2005
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      29th Jan 2005
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
>
>

 
Reply With Quote
 
Mad Scientist Jr
Guest
Posts: n/a
 
      31st Jan 2005
Thanks a bunch - this worked like a charm !

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the PictureBox downsampling for its bitmap? Sharon Microsoft C# .NET 0 16th Oct 2008 01:36 PM
PictureBox doesn't copy drawn content to Bitmap Nitin Microsoft Dot NET 1 15th Feb 2008 07:37 AM
How to Zoom on bitmap in PictureBox Anders Fredborg Microsoft Dot NET Compact Framework 1 2nd Jun 2004 11:07 PM
Drag a bitmap in a picturebox =?Utf-8?B?TWlra2VsIEFuZHJlYXNlbg==?= Microsoft Dot NET Compact Framework 1 28th Mar 2004 03:06 PM
Extract portion of a bitmap to display, not entire bitmap. Alex Gray Microsoft C# .NET 2 22nd Feb 2004 01:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:16 AM.