Creating a bitmap from Graphics

G

Guest

Hello,

I have a function that accepts an Image as the input. Currently, I load a
bitmap in the calling class and pass that in. I'd like to create an overlay
with another image and then pass the resulting bitmap in to the
aforementioned function for it to finish everything.

The problem I'm having is that after I combine the two images, I am left
with a Graphics object. I can't do a g.DrawImage at this point. I know I can
save it to a file first, but I'd rather avoid that since once it is created
and used, it isn't needed anymore. Is there any way to get a bitmap out of
the Graphics object without saving it to a file first? If not, I can always
modify the function to accept two images instead of one, but I'd rather avoid
that if possible.

Thanks,
Frij
 
K

Kevin Spencer

The System.Drawing.Graphics class that is associated with a Device Context.
The Device Context is associated with a Window. In other words, every Window
(every UI element in Windows) has a Device Context associated with it so
that the Window can be drawn. So, it is the Windows that provides the Device
Context that provides a Graphics instance for drawing the Window. In other
words, you can't take a Graphics instance from one Device Context and use it
to draw on another Device Context.

When you combine 2 images, you are combining the contents of 2 Windows. Each
has its own Device Context. Now, if the new image is the same size as one of
the 2 images you are combining, you can draw using the Graphics instance
from the destination image. If the images is a different size, you have to
create a new image, with its own Device Context, and draw on that, using the
Graphics instance for that image.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.
 

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