父图像放大时设置子图像背景 [英] Set child Image Background when Parent Image is Zoomed

查看:112
本文介绍了父图像放大时设置子图像背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像控件,可以将所选图像放大到IN和OUT。



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



在说完所有这些之后,需要将整个图像与

我已经用下面的代码实现了这个功能:

c $ c>位图bmpCopy =新位图(picEditIsdDiagram.Image);

图形画布= Graphics.FromImage(bmpCopy);
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
尝试
{
foreach(在picEditIsdDiagram.Controls中控制MeasurementSymbol)
{
if(MeasurementSymbol是DevExpress.XtraEditors.HScrollBar || MeasurementSymbol是DevExpress.XtraEditors.VScrollBar )
{
continue;
}

位图bmpControl =新位图(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; (File.Exists(destinationPath))
{
destinationPath = save.SelectedPath +\\+ IsdDiagram.Isd.Name + - + IsdDiagram(

) .Name +[+ count ++。ToString()+] .Jpeg;
}

bmpCopy.Save(destinationPath,ImageFormat.Jpeg);
SERVICE.NMessageBox.Show(以Jpeg格式保存成功图,保存图像,MessageBoxButtons.OK,MessageBoxIcon.Information);
}

catch(Exception ex)
{
SERVICE.NMessageBox.Show(导出完整图表错误:+ ex.Message.ToString( ),Error,MessageBoxButtons.OK,MessageBoxIcon.Error);
}

现在问题出现时,我放大图像,然后单击导出图表按钮。
缩放后仅显示图像的特定区域。所以控制的背景(这是控制的可见区域之外)图像被改变,并不是根据图像。



我将图像附加到进一步澄清:

缩放之前的图片:


缩放后的图片:





,您可以看到控制图像的背景与预期不符。



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

(bmpControl,new Rectangle(Point.Empty,bmpControl.Size));

解决方案

MeasurementSymbol.DrawToBitmap
bmpControl.MakeTransparent(Color.Transparent);

难以回答的问题是为什么这段代码产生一个完全透明的图像。换句话说,顶部图像是奇怪的,最下面的图像是正常的。像bmpControl这样的控件不会使用Color.Transparent绘制自己。然后再次,我们不能看到可能是什么样的控制。



您需要停止使用控件来存储这些图像。将它们存储在位图中。只有这样才能确保你不依赖于控制的善意来吸引你的希望。并确保您传递给MakeTransparent的颜色实际上匹配位图的背景颜色。如果它已经是透明的,使用支持像PNG这样的透明度的图片格式,那么根本不要调用MakeTransparent()。


I have an image control in which the image selected can be Zoomed IN and OUT.

User can place some controls over the image at run-time. After placing the controls, if user Zoom-IN the image, then the controls are not Zoomed, instead they are moved relative to their position in the image. So the position of the controls remains same on the image, just the image will be Zoomed.

After saying all that, the requirement is to export the complete image along with the controls added by user.

I have achieved this functionality with the following code:

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

Now the problem arises when i Zoom the image, and then click on the Export Diagram button. After Zooming only a specific region of the image is displayed. So the background of the Control (that is outside the visible region of the control) Image is changed and is not as per the Image.

I am attaching the Images for further clarification:

Image before Zoom:

Image after Zoom :

So in the above images, you can see that the background of the Control image is not as expected.

Can anybody help me to get the correct background even after applying the ZOOM?

解决方案

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

The hard question to answer is why this code produces an image that's transparent at all. In other words, the top image is the strange one, the bottom one is normal. A control, like bmpControl, doesn't draw itself with Color.Transparent. Then again, we can't see what kind of control that might be.

You'll need to stop using controls to store these images. Store them in a Bitmap instead. Only way to make sure that you don't depend on the kindness of a control to draw itself the way you hope. And make sure that the color you pass to MakeTransparent actually matches the background color of the bitmap. If it already is transparent, using an image format that supports transparency like PNG, then don't call MakeTransparent() at all.

这篇关于父图像放大时设置子图像背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆