473,409 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

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

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
2 6724
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
Thanks a bunch - this worked like a charm !

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mad Scientist Jr | last post by:
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...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
9
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I...
5
by: Ajith Menon | last post by:
I want to copy a part of the windows form into clipboard as .bmp file. The usecase is the user drags using the mouse and creates a rectangular selection any where on the form. This part has to be...
12
by: Lee | last post by:
I have the following code using System; using System.Windows.Forms; using System.Drawing; using System.Collections; namespace FINN {
7
by: Earl | last post by:
I have an image issue that I do not understand. When I try to save a bitmap created from a picturebox image, I can save without exception so long as the bitmap was retrieved from a file and loaded...
2
by: =?Utf-8?B?Sm9uIFBhcnJ5?= | last post by:
Hi, I've got a Picturebox, into which I am putting a frame bitmap from a video camera. The camera bitmap is larger than the picturebox, which has SizeMode set to "Zoom" which causes the bitmap...
1
by: Nitin | last post by:
Hi! I've been trying to draw in a PictureBox and save the resulting image to a Bitmap object that I save to a file on disk. Unfortunately, I can't seem to get the elements that I'm drawing to...
0
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Using VS2005 with .NET Framework 2.0 I'm using the the System.Windows.Forms.PictureBox to display a bitmap when the PictureBox.SizeMode is PictureBoxSizeMode.StretchImage The PictureBox can be...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.