By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,311 Members | 1,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,311 IT Pros & Developers. It's quick & easy.

how to save a bitmap file from a pictureBox?

P: n/a
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.
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

"charsh" <im**@21cn.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 15 '05 #2

P: n/a
Thanks.

but it doesn't work.
the whole bitmap picture is black. that means nothing inside the bitmap.
"Gerben van Loon" <no****************@home.nl> 写入消息新闻
:bf**********@news1.tilbu1.nb.home.nl...
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

Nov 15 '05 #3

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

How to create a new blank bitmap:
Bitmap bmpsave = new Bitmap(sizex,sizey,g);
How to save a bitmap: bmpsave.Save("C:\\bla.bmp",System.Drawing.Imaging. ImageFormat.Bmp);

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

"charsh" <im**@21cn.com> wrote in message
news:OO****************@TK2MSFTNGP10.phx.gbl... Thanks.

but it doesn't work.
the whole bitmap picture is black. that means nothing inside the bitmap.
"Gerben van Loon" <no****************@home.nl> 写入消息新闻
:bf**********@news1.tilbu1.nb.home.nl...
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


Nov 15 '05 #4

P: n/a
Not surprising - that code just creates an empty image

Try this fragment

pictureBox1.Image.Save("pictureBox1.bmp",ImageForm at.Bmp);

"charsh" <im**@21cn.com> wrote in message news:<OO**************@TK2MSFTNGP10.phx.gbl>...
Thanks.

but it doesn't work.
the whole bitmap picture is black. that means nothing inside the bitmap.
"Gerben van Loon" <no****************@home.nl> 写入消息新闻
:bf**********@news1.tilbu1.nb.home.nl...
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

Nov 15 '05 #5

P: n/a
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);

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

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.


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

Nov 15 '05 #6

P: n/a
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.PixelFor mat.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

"charsh" <im**@21cn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... 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 it 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);

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

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 bitmapIf you are creating a drawing type program, you can use Graphics.DrawImageto draw the bitmap instead of assigning it to the image property of the
Picture Box.

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

Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.