gpt4 book ai didi

c# - 使用 C# 合并 2 个图像

转载 作者:行者123 更新时间:2023-12-02 05:04:00 24 4
gpt4 key购买 nike

我想在我的 C# 程序中合并两张图片。第一个是灰度模式下的任何图片,第二个是这张图片:2nd Picture

两张图片/图像的大小相同,这是我的代码:

Bitmap first = new Bitmap (picturebox1.image);
Bitmap second = new Bitmap (picturebox2.image);
Bitmap result = new Bitmap (first.width, first.height);
Graphics g = Graphics.FromImage(result);
g.DrawImageUnscaled(first, 0, 0);
g.Flush();
g.DrawImageUnscaled(second, 0, 0);
g.Flush();
picturebox3.image = result;

我可以加入那些图片,但结果比两张原图小(两张图片大小相同)。谁能给我一些建议?

此外,我希望结果图片具有这样的条件: 如果第2张图片的边缘像素在第1张掉到亮的一侧,就会变暗,否则当边缘掉到暗的一侧时,就会变亮(像发光)。所以文本将是半透明的。

这是我想要的结果示例。

Result

谁能给些建议吗?

最佳答案

是为了加入

Bitmap first = new Bitmap (picturebox1.Image);
Bitmap second = new Bitmap (picturebox2.Image);
Bitmap result = new Bitmap (first.Width+first.Width, first.Height);
Graphics g = Graphics.FromImage(result);
g.DrawImageUnscaled(first, 0, 0);
g.DrawImageUnscaled(second,first.Width, 0);

尝试将一个合并到另一个之上。自己设置 alpha(红色:如果你不想要 alpha,你可以使用 BitMap.MakeTransParent)

        public Bitmap SetImageOpacity(Image image, float opacity)
{
try
{
//create a Bitmap the size of the image provided
Bitmap bmp = new Bitmap(image.Width, image.Height);

//create a graphics object from the image
using (Graphics gfx = Graphics.FromImage(bmp))
{

//create a color matrix object
ColorMatrix matrix = new ColorMatrix();

//set the opacity
matrix.Matrix33 = opacity;

//create image attributes
ImageAttributes attributes = new ImageAttributes();

//set the color(opacity) of the image
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

//now draw the image
gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
}
return bmp;
}
catch (Exception ex)
{

return null;
}
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap first = new Bitmap(pictureBox1.Image);
Bitmap second = SetImageOpacity(pictureBox2.Image, 0.5f);
//Bitmap result = new Bitmap(first.Width, first.Height);
//fix :
Bitmap result = new Bitmap(Math.Max(first.Width,second.Width), Math.Max(first.Height,second.Height));
Console.WriteLine(first.Width);
Graphics g = Graphics.FromImage(result);
g.DrawImageUnscaled(first, 0, 0);
g.DrawImageUnscaled(second, 0, 0);
pictureBox3.Image = result;
result.Save("result.jpg" );
}
}
}

来水印你为什么不想使用 Drawstring with alpha这是所有这些的文章http://www.codeproject.com/Articles/5034/How-to-implement-Alpha-blending

关于c# - 使用 C# 合并 2 个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16556848/

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