473,387 Members | 1,542 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,387 software developers and data experts.

breaking up bitmap images

Hello,

I am reading in a bitmap image and storing it as a bitmap in C#. I
need to perform some mathmatical operations on that image but it needs
to be broken up into smaller fragments (16x16). On each of these
fragments I need to perform my work, then write back the manipulated
fragment to a new image. I have tried some different techniques but so
far no luck. Any advice would be great!

Thanks.
Jan 14 '08 #1
7 8405
Stephen,

It should be easy enough. First, you create a new Bitmap instance,
passing 16x16 to indicate you want a 16x16 image. Once you have that, you
can pass the Bitmap to the static FromImage method on the Graphics class to
return a Graphics instance which will let you draw on the Bitmap. Once you
have the Graphics instance, you can then call the DrawImage method on it,
passing the original image, and the section of the original image you want
to draw on your new, smaller image.

Then you can perform your processing.

Putting the images back together is just a matter of reversing the
process. Create one image which is the size of all the smaller images, get
the Graphics instance for it, and then call DrawImage on the Graphics
instance, passing the smaller pieces and drawing them where appropriate.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<St******************@gmail.comwrote in message
news:8b**********************************@s8g2000p rg.googlegroups.com...
Hello,

I am reading in a bitmap image and storing it as a bitmap in C#. I
need to perform some mathmatical operations on that image but it needs
to be broken up into smaller fragments (16x16). On each of these
fragments I need to perform my work, then write back the manipulated
fragment to a new image. I have tried some different techniques but so
far no luck. Any advice would be great!

Thanks.

Jan 14 '08 #2
On Mon, 14 Jan 2008 07:52:59 -0800, <St******************@gmail.comwrote:
Hello,

I am reading in a bitmap image and storing it as a bitmap in C#. I
need to perform some mathmatical operations on that image but it needs
to be broken up into smaller fragments (16x16). On each of these
fragments I need to perform my work, then write back the manipulated
fragment to a new image. I have tried some different techniques but so
far no luck. Any advice would be great!
In addition to what Nicholas wrote...

It sounds as though you want some sort of distributed processing to
occur. Depending on exactly how you're distributing the processing, you
may find it better to use LockBits to create a single chunk of data that
all the processors can operate on, and give each one a specific subset to
use so that they don't conflict.

In particular, if all of this is happening within a single process, the
overhead of copying all of those 16x16 subsets could be enough to
significantly reduce or eliminate any advantages you might have gotten
from parallelizing your algorithm. Actually, it could do so even if the
processing is being distributed to multiple processes or computers, but in
that case the solution would be different (maybe use memory-mapped files
for multiple processes...for multiple computers the only practical
solution is likely to be to make the chunks of work larger than 16x16).

Pete
Jan 14 '08 #3
On Jan 14, 11:08 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Stephen,

It should be easy enough. First, you create a new Bitmap instance,
passing 16x16 to indicate you want a 16x16 image. Once you have that, you
can pass the Bitmap to the static FromImage method on the Graphics class to
return a Graphics instance which will let you draw on the Bitmap. Once you
have the Graphics instance, you can then call the DrawImage method on it,
passing the original image, and the section of the original image you want
to draw on your new, smaller image.

Then you can perform your processing.

Putting the images back together is just a matter of reversing the
process. Create one image which is the size of all the smaller images, get
the Graphics instance for it, and then call DrawImage on the Graphics
instance, passing the smaller pieces and drawing them where appropriate.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

<Stephen.Schoenber...@gmail.comwrote in message

news:8b**********************************@s8g2000p rg.googlegroups.com...
Hello,
I am reading in a bitmap image and storing it as a bitmap in C#. I
need to perform some mathmatical operations on that image but it needs
to be broken up into smaller fragments (16x16). On each of these
fragments I need to perform my work, then write back the manipulated
fragment to a new image. I have tried some different techniques but so
far no luck. Any advice would be great!
Thanks.
I am a little confused on how to use the DrawImage() for the use that
I need it...should I use Rectangle? Or PointF? Or another option????
Jan 14 '08 #4
On Mon, 14 Jan 2008 13:33:12 -0800, <St******************@gmail.comwrote:
I am a little confused on how to use the DrawImage() for the use that
I need it...should I use Rectangle? Or PointF? Or another option????
Whatever you want.

There are a lot of overloads for DrawImage(). Just find the one that
suits your needs best. For example:

Bitmap[,] BitmapsFromBitmap(Bitmap bmpSrc, int cxDst, int cyDst)
{
int ccol = (bmpSrc.Width - 1) / cxDst + 1,
crow = (bmpSrc.Height - 1) / cyDst + 1;
Bitmap[,] rgbmpRet = new Bitmap[ccol][crow];

for (int irow = 0; irow < crow; irow++)
{
for (int icol = 0; icol < ccol; icol++)
{
Bitmap bmpDst = new Bitmap(cxDst, cyDst);

using (Graphics gfx = Graphics.FromImage(bmpDst))
{
gfx.DrawImage(bmpSrc, 0, 0,
new Rectangle(icol * cxDst, irow * cyDst, cxDst,
cyDst),
GraphicsUnit.Pixel);
}

rgbmpRet[icol, irow] = bmpDst;
}
}

return rgbmpRet;
}

Caveat: the above was just typed into the message...I didn't bother to try
to compile or test it. The code also makes no attempt to deal bitmaps
that aren't an integral multiple of the size of the sub-bitmaps; if I
recall correctly, if a source that isn't an integral multiple of the
destination size is used, all that will happen is that the right and/or
bottom edge of the last sub-bitmaps just won't be painted (they'll be
black).

Finally, the above code assumes the passed in Bitmap is at least 1x1 pixel
in size.

Pete
Jan 14 '08 #5
Stephen,

I would use the overload specified here:

http://msdn2.microsoft.com/en-us/library/ms142040.aspx

You just have to define the source and destination rectangles. The
destination rectangle should be simple enough, it's just the rectangle that
defines the boundary of the whole image (the 16x16 image).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<St******************@gmail.comwrote in message
news:90**********************************@21g2000h sj.googlegroups.com...
On Jan 14, 11:08 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>Stephen,

It should be easy enough. First, you create a new Bitmap instance,
passing 16x16 to indicate you want a 16x16 image. Once you have that,
you
can pass the Bitmap to the static FromImage method on the Graphics class
to
return a Graphics instance which will let you draw on the Bitmap. Once
you
have the Graphics instance, you can then call the DrawImage method on it,
passing the original image, and the section of the original image you
want
to draw on your new, smaller image.

Then you can perform your processing.

Putting the images back together is just a matter of reversing the
process. Create one image which is the size of all the smaller images,
get
the Graphics instance for it, and then call DrawImage on the Graphics
instance, passing the smaller pieces and drawing them where appropriate.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

<Stephen.Schoenber...@gmail.comwrote in message

news:8b**********************************@s8g2000 prg.googlegroups.com...
Hello,
I am reading in a bitmap image and storing it as a bitmap in C#. I
need to perform some mathmatical operations on that image but it needs
to be broken up into smaller fragments (16x16). On each of these
fragments I need to perform my work, then write back the manipulated
fragment to a new image. I have tried some different techniques but so
far no luck. Any advice would be great!
Thanks.

I am a little confused on how to use the DrawImage() for the use that
I need it...should I use Rectangle? Or PointF? Or another option????

Jan 14 '08 #6
On Tue, 22 Jan 2008 16:49:59 -0800, <St******************@gmail.comwrote:
[...]
I have started to figure this out but I have a new dilemma that has me
confused. With the image I need to split it up into 16x16 sub images
and SAVE the coordinates of each of those subimages (say sub image 1
goes from 0,0 to 15,15 or sub image 10 goes from 150,165 to
160,175...just arbitrary examples) perform the work on each subimage
then recombine the subimages (with work performed on them) to a new
output image. Any advice/help on this would be great.
You'll either need to create a parallel data structure to contain the
coordinate information or, probably better, create a new data structure
that can contain both the image reference and the coordinate information.
That way you can correlate the coordinates with each generated image.

As for recombining, it's pretty much the inverse of the splitting. Create
a Bitmap the size you need to contain all the pieces, then iterate through
the pieces drawing each one into the destination bitmap.

Pete
Jan 23 '08 #7
On Wed, 23 Jan 2008 06:04:40 -0800, <St******************@gmail.comwrote:
used your algorithm to attempt to solve this problem and upon the
program ending I have over 8200 files...i thought I would have 4800
files because I am using a 1280x960 image which is broken up into
16x16 blocks resulting in a 80x60 = 4800 images. Make sense? Any
further advice would be great. Here is the code that works...(your
code provided was close)
I don't see anything obviously wrong. What filenames do you get when you
use the version of Save() that embeds the row and column indices in the
file name? You can't have 8200 files and still have embedded indices that
only range from "00X00" up to "79X59", so if in fact that loop is writing
more than 4800 files (I really don't see how it could be), you should see
filenames outside that range.

You should really be testing with a much small test file to start with.
It's much easier to step through the entire algorithm, and to get a handle
on exactly what's being written, when the number of tiles is small. I'd
recommend starting with something that only generates four to six tiles.
That'd give you enough to watch several iterations, but not so many that
watching _all_ the iterations would take prohibitively long.

Pete
Jan 23 '08 #8

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

Similar topics

2
by: GT | last post by:
Could someone please explain how to add images to a ListView other than in the first column?
0
by: mhospodarsky | last post by:
Hi-- I am having trouble with bitmap images displaying. They are bitmap versions of icons. I have a program that uses these bitmap images to display at certain points specified in a layer. I...
4
by: Zenon | last post by:
I have a networked Access split database which has been around for about 4 years. One table stores the path to bitmap images. This past week, one of the 3 clients lost the ability to display...
0
by: Mahil | last post by:
I have trying to read the sample database fiields in the Northwind from MS Access into an Applet. I am able to successully read and display all the text fields. But the difficulty comes with 'Photo"...
3
by: srinpraveen | last post by:
I want to put a bitmap image which I have created using Microsoft paint onto my C++ program. I am working in Turbo C++ environment. How do I implement it? I found that putimage() attribute does not...
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...
7
by: gagansingh | last post by:
guys, I really want to know how i can process images in c++ (.bmp)
6
by: lanwrangler | last post by:
I know it's a long shot but does anyone have any pointers to generic algorithms - or, even better, Python code - for comparing images and computing a value for the "difference" between them? ...
0
by: kardon33 | last post by:
Hello All, Back Ground: I need to write a program that will display a bitmap image across the top of screen while the computer is running. The bitmap is a 800x30 image. After the image is on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.