how to save a bitmap file from a pictureBox?

C

charsh

Hi,

I using the code below to draw a text in a pictureBox1.

//Start---------------------------------------------------------------
private void button1_Click(object sender, System.EventArgs e)
{
Graphics g;
g = pictureBox1.CreateGraphics();
g.FillRectangle(Brushes.White,0,0,160,160);

Font f = new Font("Arial", 14);

StringFormat format = new StringFormat (StringFormatFlags.NoClip);
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;

g.DrawString("Hello",f,Brushes.Red,40,40,format);
}
//End-------------------------------------------------------------------

My question is how to save the result(it is a "hello" in the pictureBox1) to
a bitmap file.
I mean the bitmap file will contain the "Hello" which i drawed.


thx.
 
G

Gerben van Loon

Hi,

Would be something like this:

Bitmap bmpsave = new Bitmap(sizex,sizey,g);
bmpsave.Save("C:\\bla.bmp",System.Drawing.Imaging.ImageFormat.Bmp);

hope it helps,

greets
 
C

charsh

Thanks.

but it doesn't work.
the whole bitmap picture is black. that means nothing inside the bitmap.
 
J

Jay B. Harlow [MVP - Outlook]

Charsh,
If you read Gerben's sample closely he is showing you two things:

How to create a new blank bitmap:
How to save a bitmap:
What you need now is how to get a bitmap from the Image property of the
Picture box. Unfortunately unless you assign a bitmap to the property it
will remain nothing, even if you draw on the PictureBox.

What I would do is:
- draw on a new bitmap, the first line from Gerben, not the picture box
- assign this bitmap to the image property of the Picture Box
- save this new bitmap

If you are creating a drawing type program, you can use Graphics.DrawImage
to draw the bitmap instead of assigning it to the image property of the
Picture Box.

Hope this helps
Jay
 
M

Mark Mullin

Not surprising - that code just creates an empty image

Try this fragment

pictureBox1.Image.Save("pictureBox1.bmp",ImageFormat.Bmp);
 
C

charsh

thanks Jay,

actually, becuase the GDI+ provide a great quality for rotated text.
so i got a rotated text using the following code. but i can not save the
rotated text as a
bitmap file.

//---------------------------------------------------------------------
private void button4_Click(object sender, System.EventArgs e)
{
Graphics g;

g = pictureBox2.CreateGraphics();
g.FillRectangle(Brushes.Gray,0,0,160,160);

Point Middle = new Point (40,40);
g.TranslateTransform (Middle.X, Middle.Y);

g.RotateTransform (45);
Font f = new Font("Arial", 14);

StringFormat format = new StringFormat (StringFormatFlags.NoClip);
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;


g.DrawString("I want it",f,Brushes.Red,40,20,format);

}
//------------------------------------------------------------------------


I have try the way above, but the question is how to draw a rotated text on
the new bitmap.

i think my question should be : how to save a "text" which i drawed to a
bitmap file?

charsh
 
J

Jay B. Harlow [MVP - Outlook]

charsh,
Do you know how to draw on a bitmap? I neglected to share this with you
yesterday.

Bitmap bmpsave = new
Bitmap(sizex,sizey,System.Drawing.Imaging.PixelFormat.Format24bppRgbg);
Graphics g;
g = Graphics.FromImage(bmpsave );
g.DrawString("I want it",f,Brushes.Red,40,20,format);
g.Dispose();

The Graphics.FromImage statement will return a new Graphics object that you
can draw on the bitmap with.

You should always Dispose your Graphics objects that you retrieve outside of
the Paint event. (Graphics you create from CreateGraphics, FromImage, and
the like).

Hope this helps
Jay
 

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