C#保存面板图像会产生不同的结果

时间:2015-10-28 17:55:56

标签: c#

下面是我将面板信息更改为位图的代码。 位图首先由我的面板信息生成,然后保存为图像文件。 我确认宽度,高度和边界代表我的面板给出的正确信息。 我目前不确定为什么我的结果bmp / jpeg文件与我面板上的图像不同。

//位图保存功能

  String message = String.valueOf(temp1);

//绘图功能

DECLARE @scratch AS TABLE(InspectionKey int, clmRefNumber int, clmInspType int) 

INSERT INTO tblInsp (clmInsDate, clmBandColor, clmDistrictNumbr, clmCalibrationBlock, clmTGauge, clmHarTester,clmInspecter) 
    OUTPUT INSERTED.InspectionKey, 231, 3 
    INTO @scratch 
VALUES ('1-1-15',4,6) 
INSERT INTO xtblInspRef (clmInsp, clmRefNumber, clmInspType) 
SELECT InspectionKey, clmrefnumber,clmInspType 
FROM @scratch

保存位图得到的结果。

enter image description here

我的panel1上的图像(从我的窗体中裁剪)

enter image description here

1 个答案:

答案 0 :(得分:2)

您的Bounds属性是面板与父容器的关系,因此这并不总是有效:

this.panel1.DrawToBitmap(bmp, panel1.Bounds);

请改为尝试:

this.panel1.DrawToBitmap(bmp, panel1.ClientRectangle);

您的位图大小也应该使用ClientSize属性,因为面板的宽度和高度属性包括任何边框大小:

Bitmap bmp = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height);

根据您的更新,CreateGraphics是一个临时画布,不会成为面板的一部分,因此无需保存。改为使用面板的绘画事件:

private void panel1_Paint(object sender, PaintEventArgs e) {
  using (Pen myPen = new Pen(Color.Black, 5)) {
    e.Graphics.Clear(Color.White);
    if (bCircle) {
      e.Graphics.DrawEllipse(myPen, x, y, 100, 100);
    } else if (bSquare) {
      e.Graphics.DrawRectangle(myPen, x, y, 100, 100);
    }
  }
}

要进行更新,您只需要使控件无效:

panel1.Invalidate();