Converting a Graphics object to bitmap - please help!

A

almurph

RE: Tryign to convert Graphics object to a bitmap


Hi,

Hope you can help me with this. I have to open a file and add some
text to it and then display it. So I create an Image object then
import it into a Graphics object and add the text.
I then try converting it to a bitmap image using the following
line:
Bitmap newBit = new Bitmap(440, 60, gfx);

but this does not works as when I display it the image does not
appear.

Can anyone help me please? Any suggestions/corrections/hints/code-
samples would be much appreciated.

Thank you,
Al.



**** CODE ****

Image img = Image.FromFile ("C:\a.jpg");
Font myFont = new Font("Arial", 16);
SolidBrush myBrush = new SolidBrush(Color.Black);

Graphics gfx=Graphics.FromImage(img);
gfx.DrawString("Some Data", myFont, myBrush, 100.0F, 30.0F);

Bitmap newBit = new Bitmap(440, 60, gfx);

this.pbInputImage.Image = newBit ;


**** END CODE ****
 
M

Marc Gravell

You already have an image - img.

Have you tried this.pbInputImage.Image = img;

Note that you could do with some "using" statements in there:

// *don't* dispose img; we want to give it to the
PictureBox
Image img = Image.FromFile(@"C:\a.jpg");
// but everything else is temp...
using(Font myFont = new Font("Arial", 16))
using(SolidBrush myBrush = new SolidBrush(Color.Black))
using (Graphics gfx = Graphics.FromImage(img))
{
gfx.DrawString("Some Data", myFont, myBrush, 0, 0);
}
this.pbInputImage.Image = img;

Note that if your source image is indexed, you may need to create a
new Image, create gfx from *that* image, and write both the string and
the other image (gfx.DrawImage(...)) to the new image.

Marc
 
M

Marc Gravell

Putting this together with your earlier post - something like:
// resize
Image newSizeImage;
using (Image img = Image.FromFile(@"C:\a.jpg"))
{
newSizeImage = new Bitmap(img, 440, 60);
}
// add text
using (Font myFont = new Font("Arial", 16))
using (SolidBrush myBrush = new SolidBrush(Color.Black))
using (Graphics gfx = Graphics.FromImage(newSizeImage))
{
gfx.DrawString("Some Data", myFont, myBrush, 0, 0);
}
// TODO: now use blah.Image = newSizeImage;
 
A

almurph

Putting this together with your earlier post - something like:
            // resize
            Image newSizeImage;
            using (Image img = Image.FromFile(@"C:\a.jpg"))
            {
                newSizeImage = new Bitmap(img, 440, 60);
            }
            // add text
            using (Font myFont = new Font("Arial", 16))
            using (SolidBrush myBrush = new SolidBrush(Color..Black))
            using (Graphics gfx = Graphics.FromImage(newSizeImage))
            {
                gfx.DrawString("Some Data", myFont, myBrush, 0, 0);
            }
            // TODO: now use blah.Image = newSizeImage;

Mark - ur a star - works like a charm now. Thank you very much.

Oh, btw - I have never see the "using" keyword used in such a fashion.
Can you explain it perhaps?

Thank you,
Al.
The stupid one.
 
M

Marc Gravell

Many GDI (graphics) objects are disposable - meaning that they
implement IDisposable, and should be explicitely released when you are
done with them to free the (usually unmanaged) resource [probably a
GDI handle in this case].

The "using" syntax here is a language shortcut, and

using (Font myFont = new Font("Arial", 16)) {
// some code
}
is [roughly] identical to:
Font myFont = new Font("Arial", 16));
try {
// some code
} finally {
if(myFont!=null) myFont.Dispose();
}

There are some subtle differences (actually a separate backing
variable is used etc) but that captures the essense; the main point is
that regardless of success or failure, the "finally" block ensures
that the font is disposed. I've also wrapped the brush and the
graphics object. If you were simply writing to disk, I'd also
Dispose() the newSizeImage - however, in this example we want
newSizeImage to live longer than just our method (so that the UI can
display it!), so we don't dispose this one.

The rule of thumb is: if you have responsibility for an IDisposable
object (i.e. you created it etc), then it is your job to Dispose() it
*when you are sure that you are done*. Many IDisposable objects also
have a finalizer, so the GC might also release the resource
eventually, but to be tidy why not release sooner?

Marc
 
A

almurph

Many GDI (graphics) objects are disposable - meaning that they
implement IDisposable, and should be explicitely released when you are
done with them to free the (usually unmanaged) resource [probably a
GDI handle in this case].

The "using" syntax here is a language shortcut, and

using (Font myFont = new Font("Arial", 16)) {
  // some code}

is [roughly] identical to:
Font myFont = new Font("Arial", 16));
try {
  // some code} finally {

  if(myFont!=null) myFont.Dispose();

}

There are some subtle differences (actually a separate backing
variable is used etc) but that captures the essense; the main point is
that regardless of success or failure, the "finally" block ensures
that the font is disposed. I've also wrapped the brush and the
graphics object. If you were simply writing to disk, I'd also
Dispose() the newSizeImage - however, in this example we want
newSizeImage to live longer than just our method (so that the UI can
display it!), so we don't dispose this one.

The rule of thumb is: if you have responsibility for an IDisposable
object (i.e. you created it etc), then it is your job to Dispose() it
*when you are sure that you are done*. Many IDisposable objects also
have a finalizer, so the GC might also release the resource
eventually, but to be tidy why not release sooner?

Marc

Marc - its been an education. Thank you very much sir.

Al.
 

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