473,387 Members | 1,440 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.

Converting a Graphics object to bitmap - please help!

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 ****
Feb 20 '08 #1
5 9361
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
Feb 20 '08 #2
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;
Feb 20 '08 #3
On Feb 20, 2:38*pm, Marc Gravell <marc.grav...@gmail.comwrote:
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.
Feb 20 '08 #4
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
Feb 20 '08 #5
On Feb 20, 2:52*pm, Marc Gravell <marc.grav...@gmail.comwrote:
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.
Feb 20 '08 #6

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

Similar topics

12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
5
by: Vin | last post by:
Hi, I am using the following code to draw whatever the user draws using x,y. // draws lines directly on a winform. CreateGraphics().DrawLine(APen, x, y, OldX, OldY); Now how do I save the...
2
by: Tamer Abdalla via DotNetMonster.com | last post by:
Hello, everyone! I DO need some help in order to understand how to create graphics in VB.NET. I'm a little bit confused... I once knew a time when using Point & PSet was almost the only way to...
8
by: Nathan Sokalski | last post by:
I am trying to write code to rotate a graphic that I have. Here is the code I am currently using: Dim frogbitmap As New Bitmap(Drawing.Image.FromFile(Server.MapPath("images/frog.gif"))) Dim...
1
by: placid | last post by:
Hi all, This is the following code i have, Public Class Form1 Inherits System.Windows.Forms.Form Dim A, B, c, U, x, y As Double Dim Red, Green, Blue As Integer
15
by: Hamed | last post by:
Have I posted the message to wrong newsgroup? Or Does the question is so much strage? Would someone please kindly direct me to a true newsgroup or resource? Best Regards Hamed
9
by: she_prog | last post by:
Dear All, I need to save the content of a panel to a bitmap. The panel can have many child controls which also need to be saved. The problem would be solved if I could have the panel saved to a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.