Kenneth Chang
Kenneth Chang

Reputation: 37

Save panel or Form as image with High quality

I am currently using this code to screen capture the panel I created, but whenever I am saving it, the quality is bad. Is there any way to maintain the good quality when saving it?

I tried resizing the panel but the result is still the same. I tried doing a normal screen shot with the snipping tool and it also has the same result with the codes I use.

Any suggestions? or help?

private void SaveControlImage(Control theControl)
{
    snapCount++;

    int width = panel1.Size.Width;
    int height = panel1.Size.Height;

    Bitmap bm = new Bitmap(width, height, PixelFormat.Format64bppPArgb);

    panel1.DrawToBitmap(bm, new Rectangle(0, 0, width, height));
    //bm.Save(@"D:\TestDrawToBitmap.bmp", ImageFormat.Bmp);

    bm.Save(deskTopPath + @"/Offer_" + snapCount.ToString() + "_" + DateTime.Now.ToString("yyyyMMdd") + @".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}

Like here, it looks pixelated if you compare it to what you're reading now (like this website). I tried to screen cap the form but it looks like the uploaded picture so its useless

Screenshot:

1

Upvotes: 3

Views: 3131

Answers (3)

Huỳnh
Huỳnh

Reputation: 11

Bitmap bmp= new Bitmap(controls.Width, controls.Height-50, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics Grap = Graphics.FromImage(bmp);

Grap.CopyFromScreen(PointToScreen(controls.Location).X, PointToScreen(controls.Location).Y, 0, 0, AnhDoThi.Size, CopyPixelOperation.SourceCopy);
SaveFileDialog save = new SaveFileDialog();
save.Filter = "JPEG|*.jpg";
DialogResult tl = save.ShowDialog();
if (tl == DialogResult.OK)
{
    bmp.Save(save.FileName);
    MessageBox.Show("Completed !");
}

Upvotes: 1

This is what I use to save a screenshot:

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
private void SaveControlAsImage(Control control, string path)
{
    Bitmap bitmap = new Bitmap(control.Width, control.Height);
    control.DrawToBitmap(bitmap, control.Bounds);
    using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
    {
        /* using ImageFormat.Png or ImageFormat.Bmp saves the image with better quality */
        bitmap.Save(fs, ImageFormat.Png);
    }
}

Control screenshot using Windows Snipping Tool

Control screenshot using SaveControlAsImage (ImageFormat.Png)

Control screenshot using SaveControlAsImage (ImageFormat.Jpeg)

It's not the best quality (just a little blurry), but this removes the distortion around the text and keeps the right color.

Sidenote: You're not using the parameter (theControl) passed to your method, so you might as well remove it (not recommended), or change all the calls to panel1 inside the method to theControl and call the method like

this.SaveControlImage(this.panel1);

Also, when you're accessing just a property (i.e. this.panel1.Size.Width) it's better to just call the property instead of assign it to a variable. If you're calling a method and getting a value back (which you'll be using more than once), you must assign it to a variable (i.e. int arrayCount = array.Count()).

Upvotes: 1

Georg
Georg

Reputation: 5781

Windows Forms is not using vector graphics to draw the user interface. Therefore, all you can get generically is to draw the control to a Bitmap instead of to the screen. This works independently of whether your control is visible on the screen, but there is not more than that. If you want a higher-resolution image from a Windows Forms control, the only way is to resize the control and hope that it supports zooming.

Upvotes: 0

Related Questions