在C#.net中使重叠的图片框透明

我有两个重叠的图片框。两个图片框的图像都有一些透明像素。我想通过重叠图片框的透明像素看到底部图片框。

我尝试将两个图片框的背景颜色设置为透明。但它只是将图片框的背面颜色设置为表单的背景颜色。

显然你正在使用Winforms。 是的,通过绘制父像素来模拟透明度。 哪个是表单,你只看到表单像素,堆叠效果不起作用。 有一篇知识库文章显示了解决方法。 这很痛苦。 另一种方法是不使用PictureBox控件,而只是在窗体的Paint事件中绘制图像。

考虑WPF,它有一个非常不同的渲染模型,可以轻松支持透明度。

该问题的解决方案可能各不相同,主要取决于您的技能和工作量取决于您正在处理的图像类型。 例如,如果图像总是相同的分辨率,大小和重叠图像支持透明度,您可以尝试操作两个Image对象并在另一个上绘制一个,然后在PictureBox显示它。 或者,如果您需要在应用程序的不同位置多次执行此操作,您甚至可以考虑创建自己的UserContriol

回答这个问题的 ResizeImage ,特别是ResizeImage方法,展示如何创建resize,质量好的图像,所有你需要的就是改变它一点。 使其获得两个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); 

但这是唯一的例子,你必须根据自己的需要进行调整。 祝好运。

如果它是另一个PictureBox,你可以使用:

innerPictureBox.SendToBack(); innerPictureBox.Parent = outerPictureBox;