【问题标题】:Save panel or Form as image with High quality将面板或表单保存为高质量图像
【发布时间】:2018-11-20 10:10:19
【问题描述】:

我目前正在使用此代码来截屏我创建的面板,但每当我保存它时,质量都很差。保存时有什么方法可以保持良好的质量吗?

我尝试调整面板大小,但结果仍然相同。 我试着用截图工具做一个普通的屏幕截图,我使用的代码也有同样的结果。

有什么建议吗?或帮助?

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);
}

就像这里一样,如果你将它与你现在正在阅读的内容(比如这个网站)进行比较,它看起来像像素化了。我试图截屏,但它看起来像上传的图片,所以没用

截图:

【问题讨论】:

  • 也许可以尝试像超级采样这样的方法,并将你的宽度和高度乘以我建议在 4-16 左右的某个因子。不确定它是否会缩放或实际以更高分辨率渲染控件,但值得一试。
  • I tried doing a normal screen shot with the snipping tool and it also has the same result with the codes I use > 你的意思是你的表单截图已经很糟糕了吗?那么是不是表格本身已经“看起来很糟糕”?我的意思是,除非您以有损格式保存屏幕截图,否则您的表单的任何屏幕截图实际上都应该与您的表单在屏幕上显示的完全一样......
  • @KennethChang 你能添加一些截图吗? (当然还有结果)
  • 你试过.png格式吗?如果压缩模型更适合这种位图。
  • 截图包含文字,因此绝不能另存为jpg,仅用于照片。改用 png 。另外:屏幕上的文本通常是抗锯齿的,以使其看起来更窒息。有时,当脱离上下文并放大时,这看起来不会那么好..

标签: c# winforms


【解决方案1】:
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 !");
}

【讨论】:

    【解决方案2】:

    这是我用来保存屏幕截图的:

    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)

    这不是最好的质量(只是有点模糊),但这可以消除文本周围的失真并保持正确的颜色。

    旁注:您没有使用传递给您的方法的参数(theControl),因此您不妨将其删除(不推荐),或者将方法内对panel1的所有调用更改为theControl并调用类似的方法

    this.SaveControlImage(this.panel1);
    

    此外,当您只访问一个属性(即this.panel1.Size.Width)时,最好只调用该属性而不是将其分配给一个变量。如果您正在调用一个方法并取回一个值(您将多次使用该值),则必须将其分配给一个变量(即int arrayCount = array.Count())。

    【讨论】:

      【解决方案3】:

      Windows 窗体不使用矢量图形来绘制用户界面。因此,您通常可以获得的只是将控件绘制到位图而不是屏幕上。这与您的控件在屏幕上是否可见无关,但仅此而已。如果您想从 Windows 窗体控件中获得更高分辨率的图像,唯一的方法是调整控件的大小并希望它支持缩放。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-16
        • 1970-01-01
        • 2018-02-16
        • 1970-01-01
        • 2020-06-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多