图片合并问题。请帮忙

好奇都是要学的 2021-03-15 04:06:31
我有二张图片
Bitmap bmp1 = new Bitmap(Server.MapPath("../UpFile/Hc/abc.png")); 上层图片
Bitmap bmp2 = new Bitmap(Server.MapPath("../UpFile/BeiJing/3.png")); 下层图片
我想 把 bmp1 图片 放到 bmp2 上面。 切不要bmp1 图片的背景图。 就好像是把1图片画到2上面是的。而不是一图片把二覆盖了
Graphics g = Graphics.FromImage(bmp1)
g.DrawImage(bmp2, rect, new Rectangle(0, 0, bmp2.Width, bmp2.Height), GraphicsUnit.Pixel);
请问如何能做到。

图1是下层图,图2是上层图,我想做到图3效果。 现在是4图效果
...全文
408 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 3 楼 兔子党逍遥 的回复:
给你推荐一个demo大全,n久之前我上传的,绘图的功能代码基本都全了,包括滤镜,你可以下载,收藏,做参考。
推荐给我呗 还有 能讲下 g.DrawImage(bmp2, new Rectangle(0, 0, 100, 100), 0, 0, bmp2.Width, bmp2.Height, GraphicsUnit.Pixel, attributes); 我把上面代码改成 g.DrawImage(bmp2, new Rectangle(0, 0, bmp1.Width, bmp1.Height), 0, 0, bmp2.Width, bmp2.Height, GraphicsUnit.Pixel, attributes); 就是我要的了 我理解是 在bmp1上画个矩形 , 然后把 bmp2 画上 bmp1 的矩形里 然后设置 坐标 和宽度。 0, 0, bmp2.Width, bmp2.Height, GraphicsUnit.Pixel, attributes 为什么 0 0后面的 的宽高 是bmp2的 高宽那。 我理解应该是 bmp1的高宽啊
  • 打赏
  • 举报
回复
引用 3 楼 兔子党逍遥 的回复:
给你推荐一个demo大全,n久之前我上传的,绘图的功能代码基本都全了,包括滤镜,你可以下载,收藏,做参考。
推家到哪里了
  • 打赏
  • 举报
回复
引用 7 楼 兔子党逍遥 的回复:
c# GDI+ 完整例子 奇怪了,之前贴的没了,莫非csdn不允许贴自己下载资源?
我私信你了。 你可以加我吗? 我问点图片问题。最近有个项目 都是图片生成问题
兔子-顾问 2021-03-16
  • 打赏
  • 举报
回复
c# GDI+ 完整例子 奇怪了,之前贴的没了,莫非csdn不允许贴自己下载资源?
desperaso 2021-03-15
  • 打赏
  • 举报
回复
将bmp1(为png图像)的背景设为透明
要么
bitmap.MakeTransparent
要么
bitmap.SetPixel
要么

/// <summary>
/// 替换颜色
/// </summary>
/// <param name="tr">红值</param>
/// <param name="tg">绿值</param>
/// <param name="tb">蓝值</param>
/// <param name="src">替换的图像</param>
/// <param name="opacity">整体透明度</param>
/// <param name="trans">替换为透明度</param>
/// <returns></returns>
public Bitmap ReplaceToColor(int tr, int tg, int tb, Bitmap src, int opacity, int trans)
{
int w = src.Width;
int h = src.Height;
Bitmap dstBitmap = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
System.Drawing.Imaging.BitmapData srcData = src.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* pOut = (byte*)dstData.Scan0.ToPointer();
byte* p;
int stride = srcData.Stride;
int r, g, b;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
p = pIn;
b = pIn[0];
g = pIn[1];
r = pIn[2];
pOut[1] = (byte)g;
pOut[2] = (byte)r;

if (r == tr && g == tg && b == tb)
{
pOut[3] = (byte)trans;
}
else
{
dots.Add(new Dot(new Point(x, y)));
pOut[3] = (byte)opacity;
}

pOut[0] = (byte)b;
pIn += 4;
pOut += 4;
}
pIn += srcData.Stride - w * 4;
pOut += srcData.Stride - w * 4;
}
src.UnlockBits(srcData);
dstBitmap.UnlockBits(dstData);
return dstBitmap;
}
}
兔子-顾问 2021-03-15
  • 打赏
  • 举报
回复
public void DrawTransparent()
{
    Bitmap bmp1 = new Bitmap(100, 100);
    //随便截图绘制个底图
    using (Graphics g = Graphics.FromImage(bmp1))
    {
        Rectangle ScreenArea = Screen.GetWorkingArea(Screen.PrimaryScreen.WorkingArea);
        g.CopyFromScreen(0, 0, 0, 0, new Size(ScreenArea.Width, ScreenArea.Height));
    }
    Bitmap bmp2 = new Bitmap(100, 100);
    //弄个新bmp,画个“王”
    using (Graphics g = Graphics.FromImage(bmp2))
    {
        g.Clear(Color.White);
        g.DrawString("王", SystemFonts.DefaultFont, Brushes.Yellow, 20, 20);
    }
    //绘制过去
    using (Graphics g = Graphics.FromImage(bmp1))
    {
        System.Drawing.Imaging.ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes();
        attributes.SetColorKey(bmp2.GetPixel(0, 0), bmp2.GetPixel(0, 0));//取bmp2左上角像素作为透明背景
        g.DrawImage(bmp2, new Rectangle(0, 0, 100, 100), 0, 0, bmp2.Width, bmp2.Height, GraphicsUnit.Pixel, attributes);
    }
    bmp1.Save("demo.png", System.Drawing.Imaging.ImageFormat.Png);
}
针对你的问题,写了个demo
兔子-顾问 2021-03-15
  • 打赏
  • 举报
回复
给你推荐一个demo大全,n久之前我上传的,绘图的功能代码基本都全了,包括滤镜,你可以下载,收藏,做参考。
Kim_Du 2021-03-15
  • 打赏
  • 举报
回复
按照你自己的功能,上层图片背景色置为透明,然后再底层图片的基础上,按照你想要的位置重新绘制已经置为透明背景的上层图片,得到的应该就是你想要的,思路就是这样

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧