1

我有一个图像控件,其中选择的图像可以放大和缩小。

用户可以在运行时对图像进行一些控制。放置控件后,如果用户放大图像,则控件不会缩放,而是相对于它们在图像中的位置移动。所以控件的位置在图像上保持不变,只是图像会被缩放。

说了这么多,要求是导出完整的图像以及用户添加的控件。

我已经使用以下代码实现了此功能:

Bitmap bmpCopy = new Bitmap(picEditIsdDiagram.Image);

Graphics canvas = Graphics.FromImage(bmpCopy);
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
try
{
    foreach (Control MeasurementSymbol in picEditIsdDiagram.Controls)
    {
        if (MeasurementSymbol is DevExpress.XtraEditors.HScrollBar || MeasurementSymbol is DevExpress.XtraEditors.VScrollBar)
        {
            continue;
        }

        Bitmap bmpControl = new Bitmap(MeasurementSymbol.Width, MeasurementSymbol.Height);
        MeasurementSymbol.DrawToBitmap(bmpControl, new Rectangle(Point.Empty, bmpControl.Size));
        bmpControl.MakeTransparent(Color.Transparent);

        canvas.DrawImage(
                            bmpCopy,
                            new Rectangle(0, 0, bmpCopy.Width, bmpCopy.Height),
                            new Rectangle(0, 0, bmpCopy.Width, bmpCopy.Height),
                            GraphicsUnit.Pixel
                        );

        canvas.DrawImage(bmpControl, ((UcMeasurementSymbol)MeasurementSymbol).PointInImage);
        canvas.Save();
    }

    FolderBrowserDialog save = new FolderBrowserDialog();

    save.RootFolder = Environment.SpecialFolder.Desktop;

    if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        int count = 1;

        string destinationPath = save.SelectedPath + "\\" + IsdDiagram.Isd.Name + " - " + IsdDiagram.Name + ".Jpeg";

        while (File.Exists(destinationPath))
        {
            destinationPath = save.SelectedPath + "\\" + IsdDiagram.Isd.Name + " - " + IsdDiagram.Name + " [" + count++.ToString() + "].Jpeg";
        }

        bmpCopy.Save(destinationPath, ImageFormat.Jpeg);
        SERVICE.NMessageBox.Show("Complete Diagram saved successfully in Jpeg format", "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}
catch (Exception ex)
{
    SERVICE.NMessageBox.Show("Error exporting complete Diagram. Error :" + ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

现在,当我缩放图像时出现问题,然后单击“导出图表”按钮。缩放后仅显示图像的特定区域。因此,控件的背景(即控件的可见区域之外)图像已更改,并且与图像不同。

我附上图片以作进一步说明:

缩放前的图像:

在此处输入图像描述

缩放后的图像:

在此处输入图像描述

所以在上面的图片中,你可以看到Control图片的背景并不像预期的那样。

即使在应用 ZOOM 之后,任何人都可以帮助我获得正确的背景吗?

4

1 回答 1

0
    MeasurementSymbol.DrawToBitmap(bmpControl, new Rectangle(Point.Empty, bmpControl.Size));
    bmpControl.MakeTransparent(Color.Transparent);

很难回答的问题是为什么这段代码会生成一个完全透明的图像。换句话说,上面的图像是奇怪的图像,下面的图像是正常的。控件,如 bmpControl,不使用 Color.Transparent 绘制自身。再说一次,我们看不到可能是哪种控制。

您需要停止使用控件来存储这些图像。而是将它们存储在位图中。确保您不依赖控件的善意以您希望的方式绘制自己的唯一方法。并确保您传递给 MakeTransparent 的颜色实际上与位图的背景颜色相匹配。如果它已经是透明的,使用像 PNG 这样支持透明度的图像格式,那么根本不要调用 MakeTransparent()。

于 2013-10-09T10:38:18.007 回答