问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/19268410/set-child-image-background-when-parent-image-is-zoomed