gpt4 book ai didi

c# - 是否有从资源中覆盖透明图像的解决方法?

转载 作者:太空宇宙 更新时间:2023-11-03 16:07:46 24 4
gpt4 key购买 nike

现在我有一个带有 PictureBox 的表单。我正在使用两个部分透明的图像并尝试将一个放在另一个之上。

  • sideMenuWide = 具有透明背景的圆角矩形 (.png) [底部图像]
  • labelPointer = 具有透明背景的三角形 (.png) [顶部图像]

这是我的方法:

// METHOD #1 //
Image img1 = Image.FromFile(@"C:\sideMenuWide.png");
Image img2 = Image.FromFile(@"C:\labelPointer.png");
picBox.Image = CombineImages(img1, img2);

// METHOD #2 //
Image imgA = RBS.Properties.Resources.sideMenuWide;
Image imgB = RBS.Properties.Resources.labelPointer;
picBox.Image = CombineImages(imgA, imgB);

还有CombineImage函数:(这个函数我没写,只是修改)

public static Bitmap CombineImages(Image imgA, Image imgB)
{
//a holder for the result (By default, use the first image as the main size)
Bitmap result = new Bitmap(imgA.Size.Width, imgA.Size.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(imgA, 0, 0, imgA.Width, imgA.Height);
graphics.DrawImage(imgB, 100, 70, imgB.Width, imgB.Height);
}
return result;
}

方法 #1 确实如我所愿,在 imgA 之上完美显示 imgB。

方法 #2 但是显示 imgA 很好,但 imgB 真的很微弱。

关于如何克服这个问题的任何想法?我希望能够使用资源来执行此操作,而不必从文件中提取。

最佳答案

如果文件在加载时工作,但资源不工作,那么这听起来像是 resx 构建过程或 ResourceManager 正在做一些不受欢迎的事情。我会尝试嵌入文件并直接从流中读取它们,而不是依赖 ResourceManager 为您完成。

  • 在您的解决方案资源管理器中,添加现有文件 将文件添加到您的项目中。获取添加文件的属性并将其设置为 Embedded Resource=。 (不要将其添加到 resx 文件中)

  • 在您的代码中,您现在可以获得包含文件数据的流:

    Stream instream = Assembly.GetExecutingAssembly().
    GetManifestResourceStream("RBS.labelPointer.png"));

    (提示:编译器为嵌入式资源生成一个钝名称,因此在添加文件后您可以添加临时代码来调用 Assembly.GetExecutingAssembly().GetManifestResourceNames() 以获取所有资源的列表并找到您感兴趣的文件)

  • 从流中加载位图/图像:

    Image img2 = new Bitmap(inStream);

关于c# - 是否有从资源中覆盖透明图像的解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18663992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com