I'm new to C# and Windows Forms and I would like to save a PictureBox
with labels in it in JPEG format.
This is my code so far:
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "JPG(*.JPG)|*.jpg";
if(sfd.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image.Save(sfd.FileName,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
There are labels in the picturebox, but they are not saved as well. Do you have any idea?
The most simple option is adding those labels to PictureBox
control. Then using DrawToBitmap
you can draw those labels and the image to a bitmap:
var bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(bm, new Rectangle(0,0,pictureBox1.Width, pictureBox1.Height));
Then save the bitmap in any format which you need.
Note 1: Don't forget to dispose the bitmap if you don't need it after saving.
Note 2: DrawBitmap
draws the labels just if you add them to the Controls
collection of the PictureBox
:
var label1 = new Label() {
Text = "Some Text",
BackColor = Color.Transparent,
Location = new Point(10, 10)
};
pictureBox1.Controls.Add(label1);