c# - 为什么我的位图保存为两边都有黑条的 jpg?

标签 c# forms panel image jpeg

我的程序在表单中有一个 splitcontainer。 Panel1 有一个按钮,单击该按钮应将 panel2 保存为图片(最好是 jpg)。我使用的代码有几个问题。

private void btnSave_Click(object sender, EventArgs e)
    {          
        Bitmap bmp = new Bitmap(splitContainer1.Panel2.Width * 2, splitContainer1.Panel2.Height);
        splitContainer1.Panel2.DrawToBitmap(bmp, splitContainer1.Panel2.Bounds);
        bmp.Save(@"C:\Test.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
    }

以下是我很好奇的代码问题。

  1. 如果我按原样使用它,我会得到一个正确保存的 jpg 文件,但它的两侧包含额外的黑色。 enter image description here
  2. 如果我更改代码的最后一行以省略 System.Drawing.Imaging.ImageFormat.Jpeg,我会得到一张两边都有两个透明框的图片,这仍然不是我想要的。 (它们很清楚,但它们仍然存在)。 enter image description here

如何只保存 jpg 文件而没有面板周围的额外区域?

编辑: 有几个人指出了我对 Splitcontainer1.panel2.width * 2 的调用。如果我不将宽度乘以二,我只会得到实际 panel2 的一半,如下所示,它仍然有不需要的黑条/透明条: enter image description here

最佳答案

看起来你明确地使 Bitmap 太宽了:

splitContainer1.Panel2.Width * 2

只要做到:

splitContainer1.Panel2.Width

此外,您指定的边界不正确。它们代表要在位图中绘制的矩形。您的调用可能应该是:

Bitmap bmp = new Bitmap(splitContainer1.Panel2.Width, splitContainer1.Panel2.Height);
splitContainer1.Panel2.DrawToBitmap(bmp, new Rectangle(0,0,splitContainer1.Panel2.Bounds.Width,splitContainer1.Panel2.Bounds.Height));

关于c# - 为什么我的位图保存为两边都有黑条的 jpg?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8748863/

相关文章:

JavaScript 验证表单的考试编号

C# 按钮不重新定位自己

c# - 让 HomeController 继承自具有 session 属性的 BaseController

c# - 如何让计时器运行 10 分钟然后禁用按钮?

c# - 找不到类型或命名空间名称 'Bussen'(您是否缺少 using 指令或程序集引用?)

extjs4 - 在 ext.grid.panel 的 'tools' 配置选项中显示自定义图像

C# Flow Layout Panel 换行符或新行

c# - C#计时器2同时执行的 Action

c# - : The first instance of an app will execute a process, second instance will kill the process later 怎么办

javascript - jQuery 重新加载论坛后表单提交失败