473,662 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 programmaticall y. 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(iLo op)
next
end sub

Sub TileBreaker(ByV al 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.b mp"
pb1.Image = pb1.Image.FromF ile(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(i X, 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.ToStr ing
sFile = "C:\temp\images \image" & sNumber.Substri ng(sNumber.Leng th - 3,
3) & ".bmp"
pb1.Image.Save( sFile, System.Drawing. Imaging.ImageFo rmat.Bmp) ' save
the cropped image
end sub ' TileBreaker

Nov 21 '05 #1
2 6757
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:\Imag es.bmp")
'Get 32x32 Images
dim format as pixelformat = bm.PixelFormat
v_Img32.ImageSi ze = New Size(32, 32)
v_Img32.Transpa rentColor = 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(cl oneRect, 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 programmaticall y. 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(iLo op)
next
end sub

Sub TileBreaker(ByV al 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.b mp"
pb1.Image = pb1.Image.FromF ile(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(i X, 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.ToStr ing
sFile = "C:\temp\images \image" & sNumber.Substri ng(sNumber.Leng th - 3,
3) & ".bmp"
pb1.Image.Save( sFile, System.Drawing. Imaging.ImageFo rmat.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
343
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 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...
7
11614
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 is proving to be more difficult. These pictureboxes are bound to an AccessDB. If the user wants to add an image, they select an image using an OpenFileDialog: Dim result As DialogResult = Pic_Sel.ShowDialog() If (result = DialogResult.OK) Then
9
3170
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 want to be able to draw lines in a picturebox based upon certain data points I have received. I dragged a picturebox from the toolbar onto my form, but after having gone through the help files, looking online and trying a variety of things, I...
5
4492
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 copied into clipboard. Can anybody help me in implementing this using C#? Thanks, Ajith
12
4031
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
2063
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 into the picturebox. But if I load the image from the database into the picturebox and try to save (without change), I then get a null exception telling me that the encoder parameter is null. I'm speculating that the file provides the encoder...
2
8595
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 to be resized and correctly displayed. My question is whether letting the Picturebox do the bitmap resizing is the most efficient mechanism, or whether it would be preferable to perfrom the resizing myself before passing the modified image to...
1
2201
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 appear in the file. To make sure that I'm getting something that actually is from the PictureBox, I set the background color of the PictureBox and that's all that appears in the file (I use Windows Preview). Below is the code extract that I used...
0
1399
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 resized by the user. When the PictureBox size is less then the bitmap size then the bitmap is resized by the PictureBox to a smaller size to fit the PictureBox size. My question is: What method is used to downsize the bitmap?
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8856
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8545
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7365
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5653
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.