working with smaller images created from big images

W

wvueagle84

I have the following code in which I am attempting to break a large
image up in 16x16 blocks and perform some processing on those blocks.
For debugging I would like to save each of the 16x16 blocks just to
see things working. The original image needs to stay preserved and not
changed through this function. If possible I need to take the graphics/
image object and save it back as a temporary bitmap in the system so I
can perform my image processing algorithms on it. I need to then save
the coordinates of each of these blocks. I stumped on how to make this
work and have been through many attempts...feel I am close but not
quite there yet. Advice would be great.

function xyz (Bitmap bmpSource, int width, int height)
{
int i, j;

for (y = 0; y < width; y++)
{
for (z = 0; z < height; z++)
{
Rectangle rSource = new Rectangle(y, z, 16, 16);
Rectangle rDest = new Rectangle(0, 0, 15, 15);

using (Graphics g = Graphics.FromImage(bmpSource))
{

g.DrawImage(bmpSource,rDest,rSource,GraphicsUnit.Pixel);
g.Save();
}
for (i = 0; i < y; i++)
{
for (j = 0; j < z; j++)
{
Color pixelColor = bmpSource.GetPixel(i,
j);
int r = pixelColor.R; // the Red component
int g = pixelColor.G; // the Green
component
int b = pixelColor.B; // the Blue
component
Color newColor = Color.FromArgb(r, g, b);
output.SetPixel(i, j, newColor);
output.Save(string.Format("C:\\temp\
\outputOF{0}X{1}.bmp", y, z));
}
}
z += 15;
}
y += 15;
}


}
 
P

Peter Duniho

I have the following code in which I am attempting to break a large
image up in 16x16 blocks and perform some processing on those blocks.
For debugging I would like to save each of the 16x16 blocks just to
see things working. The original image needs to stay preserved and not
changed through this function. If possible I need to take the graphics/
image object and save it back as a temporary bitmap in the system so I
can perform my image processing algorithms on it. I need to then save
the coordinates of each of these blocks. I stumped on how to make this
work and have been through many attempts...feel I am close but not
quite there yet. Advice would be great.

Well, you never closed the loop on the discussion in the previous thread..
IMHO, you were probably closer to whatever goal you have with the code in
that thread, than with the code you just posted.

Some oddities with respect to the code you just posted:

Inside the second loop you initialize two rectangles that appear to be
intended as source and destination rectangles. Why is one rectangle 16x16
while the other is 15x15? Do you really intend for the two rectangles to
not be the same size?

What is the point of using GetPixel() and SetPixel()? You've already been
given code that uses Graphics.FromImage() and Graphics.DrawImage() to do
an efficient bit-copy between two images. That technique even shows up in
the code you posted. What purpose do you have in doing a pixel-by-pixel
copy of the image?

Why are you incrementing your z and y loop variables twice? Why not just
put "y += 16" and "z += 16" inside the loop declaration and leave out the
extra math at the end of the loop?

What's the purpose of calling Graphics.Save()? Are you aware that this
has nothing at all to do with writing data to a file?

Why are you apparently saving an image every time you set a new pixel
color? This assumes that "output" is declared somewhere else accessible
and represents a Bitmap instance that can be saved. Even assuming there's
a good reason for setting each pixel individually, you're writing the file
out 256 times as often as you presumably need to be (that is, for every
file that gets written, you actually write a file 256 times).

And finally, do you have a question? Your most recent post doesn't really
provide anything in the way of a specific detail that you feel you're
having trouble with, or a specific question that might be answered.

I suppose one approach would be to read your mind to figure out the exact
behavior you want, and then just write the code for you to do that. But
a) I'm not sure I can read minds well enough to do that, and b) I think it
will be more productive in the long run if you are able to work through
the problem yourself with help and advice from others, so that you have a
better understanding of how the final implementation actually works.

So, could you please try again, and see if you can figure out a way to
formulate your question as something specific and direct, that doesn't
require any inference on our part and which includes some specific
question that can be answered? I think that if you can do that, you'll
get much better assistance than what we might be able to offer otherwise..

Pete
 
W

wvueagle84

Well, you never closed the loop on the discussion in the previous thread.
IMHO, you were probably closer to whatever goal you have with the code in
that thread, than with the code you just posted.

Some oddities with respect to the code you just posted:

Inside the second loop you initialize two rectangles that appear to be
intended as source and destination rectangles. Why is one rectangle 16x16
while the other is 15x15? Do you really intend for the two rectangles to
not be the same size?

What is the point of using GetPixel() and SetPixel()? You've already been
given code that uses Graphics.FromImage() and Graphics.DrawImage() to do
an efficient bit-copy between two images. That technique even shows up in
the code you posted. What purpose do you have in doing a pixel-by-pixel
copy of the image?

Why are you incrementing your z and y loop variables twice? Why not just
put "y += 16" and "z += 16" inside the loop declaration and leave out the
extra math at the end of the loop?

What's the purpose of calling Graphics.Save()? Are you aware that this
has nothing at all to do with writing data to a file?

Why are you apparently saving an image every time you set a new pixel
color? This assumes that "output" is declared somewhere else accessible
and represents a Bitmap instance that can be saved. Even assuming there's
a good reason for setting each pixel individually, you're writing the file
out 256 times as often as you presumably need to be (that is, for every
file that gets written, you actually write a file 256 times).

And finally, do you have a question? Your most recent post doesn't really
provide anything in the way of a specific detail that you feel you're
having trouble with, or a specific question that might be answered.

I suppose one approach would be to read your mind to figure out the exact
behavior you want, and then just write the code for you to do that. But
a) I'm not sure I can read minds well enough to do that, and b) I think it
will be more productive in the long run if you are able to work through
the problem yourself with help and advice from others, so that you have a
better understanding of how the final implementation actually works.

So, could you please try again, and see if you can figure out a way to
formulate your question as something specific and direct, that doesn't
require any inference on our part and which includes some specific
question that can be answered? I think that if you can do that, you'll
get much better assistance than what we might be able to offer otherwise.

Pete

Problems have been solved. Light bulb came on!
 
P

Peter Duniho

[..]
Problems have been solved. Light bulb came on!

And? Those problems were?

It's customary to, having answered your own question yourself, to post the
answer as a follow-up. This does a couple of things: it helps others to
understand better what your question was in the first place (not always
needed, but in this case I think it would be helpful), and more
importantly it provides a record of your answer in case someone else has a
similar issue and comes across your question searching an archive of the
newsgroup.

Also, it's considered polite behavior to do so.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top