在C#.net中使重叠的图片框透明 [英] Make overlapping picturebox transparent in C#.net
问题描述
我有两个重叠的图片框.两个图片框的图像都有一些透明像素.我想通过重叠图片框的透明像素看到底部的图片框.
I have two overlapping pictureboxes.The images of both picture boxes have some transparent pixels.I want to see the bottom picture box through the transparent pixels of the overlapping picture box.
我尝试将两个图片框的背景色设置为透明,但是只是将图片框的背景色设置为表单的背景色.
I tried setting the background color of both picture boxes as transparent.But it just sets the back color of the picture box to the background color of the form.
推荐答案
该问题的解决方案可能多种多样,这主要取决于您的技能,工作量将取决于您要处理的图像的种类.例如,如果图像始终具有相同的分辨率,大小并且重叠的图像支持透明度,则可以尝试对两个 Image
对象进行操作并在另一个对象上绘制,然后在 PictureBox
中显示它.或者,如果您需要在应用程序的不同位置多次执行此操作,甚至可以考虑创建自己的 UserContriol
.
Solutions to that problem might be various, and it mainly depends on your skills and amount of work will depend on kind of images you're dealing with. For example if images are always same resolution, size and overlapping image supports transparency you could try to do manipulation of two Image
objects and draw one over another, then display it in PictureBox
. Or if you will need to do it multiple times in various places of your app you could even consider creating your own UserContriol
.
回答此问题的代码,方法 ResizeImage
>特别是,展示如何创建调整大小的高质量图像,您所要做的就是对其稍作更改.使其获得两个 Images
作为输入参数,并对其进行更改以在另一个图像上绘制一个图像.
Code in answer of this question, method ResizeImage
in particular, show how to create resized, good quality image, all you need it is to change it a little. Make it to get two Images
as input parameters, and change it to draw one image over another.
更改可能看起来像这样
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;
}
并使用它,例如,像这样:
And use it, for example, like this:
pictureBox1.Image = CombineAndResizeTwoImages(Image.FromFile("c:\\a.png"), Image.FromFile("c:\\b.png"), 100,100);
但这只是一个例子,您必须根据需要进行调整.祝你好运.
But that its only example, and you must tune it up to your needs. Good luck.
这篇关于在C#.net中使重叠的图片框透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!