我有两个重叠的图片框。两个图片框中的图片都有一些透明像素。我想通过重叠图片框的透明像素看到下面的图片框。
我尝试将两个图片框的背景颜色设置为透明。但这只是将图片框的背景颜色设置为窗体的背景颜色。
我尝试将两个图片框的背景颜色设置为透明。但这只是将图片框的背景颜色设置为窗体的背景颜色。
Image
对象进行操纵并将一个画在另一个上面,然后在PictureBox
中显示它。或者,如果您需要在应用程序的多个位置多次执行此操作,您甚至可以考虑创建自己的UserContriol
。
这个问题的答案代码中的ResizeImage
方法特别展示了如何创建调整大小和高质量的图像,您只需稍作修改即可。将其更改为接受两个Images
作为输入参数,并将其更改为将一个图像绘制在另一个图像之上即可。 public static Bitmap CombineAndResizeTwoImages(Image image1, Image image2, int width, int height)
{
//a holder for the result
Bitmap result = new Bitmap(width, height);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the images into the target bitmap
graphics.DrawImage(image1, 0, 0, result.Width, result.Height);
graphics.DrawImage(image2, 0, 0, result.Width, result.Height);
}
//return the resulting bitmap
return result;
}
例如,可以像这样使用它:
pictureBox1.Image = CombineAndResizeTwoImages(Image.FromFile("c:\\a.png"), Image.FromFile("c:\\b.png"), 100,100);
但这只是一个例子,你需要根据自己的需求进行调整。 祝你好运。
显然你正在使用Winforms。是的,透明度是通过绘制父级像素来模拟的。也就是说,你只能看到窗体像素,堆叠效果不起作用。有一篇KB article介绍了解决方法,但这很繁琐。另一种方法是不使用PictureBox控件,而是在窗体的Paint事件中绘制图像。
考虑使用WPF,它具有非常不同的渲染模型,可以轻松支持透明度。
innerPictureBox.SendToBack();
innerPictureBox.Parent = outerPictureBox;