C# png带透明图片与jpg图片叠加处理

独醒子 2014-04-29 04:22:04
一个是png图片,比如是一个中间圆形透明区域的图片,带透明渐变。
一个是jpg图片,普通人物照片。

png在上,jpg在下叠加

合成结果就是中间圆形透明部分显示出下面的人像。
求代码,或告诉我该如何做?
谢谢
...全文
689 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kxyzjm62 2014-04-30
  • 打赏
  • 举报
回复
Ki1381 2014-04-29
  • 打赏
  • 举报
回复
先构造一个ColorMatrix,然后应用到ImageAttributes的SetColorMatrix方法。
失落的神庙 2014-04-29
  • 打赏
  • 举报
回复
用这个 JoinMImage方法就行
失落的神庙 2014-04-29
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace xxx
{
    class ImageEx
    {
        /// <summary>
        /// 缩小图片
        /// </summary>
        /// <param name="strOldPic">源图文件名(包括路径)</param>
        /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
        /// <param name="intWidth">缩小至宽度</param>
        /// <param name="intHeight">缩小至高度</param>
        public System.Drawing.Bitmap SmallPic(Bitmap strOldPic,int intWidth, int intHeight)
        {
            Bitmap objNewPic;
            try
            {
                objNewPic = new Bitmap(strOldPic, intWidth, intHeight);
                return objNewPic;
            }
            catch (Exception exp) { throw exp; }
        }

        /// <summary>
        /// 按比例缩小图片,自动计算高度
        /// </summary>
        /// <param name="strOldPic">源图文件名(包括路径)</param>
        /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
        /// <param name="intWidth">缩小至宽度</param>
        public Bitmap SmallPic(Bitmap strOldPic, int intWidth)
        {

            Bitmap objNewPic;
            try
            {
                int intHeight = (int)(((intWidth * 1.0000) / strOldPic.Width) * strOldPic.Height);
                objNewPic = new Bitmap(strOldPic, intWidth, intHeight);
                return objNewPic;
            }
            catch (Exception exp) { throw exp; }
        }


        /// <summary>
        /// 按比例缩小图片,自动计算宽度
        /// </summary>
        /// <param name="strOldPic">源图文件名(包括路径)</param>
        /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
        /// <param name="intHeight">缩小至高度</param>
        public Bitmap SmallPicH(Bitmap strOldPic, int intHeight)
        {
            Bitmap objNewPic;
            try
            {
                int intWidth = (int)(((intHeight * 1.0000) / strOldPic.Height) * strOldPic.Width);
                objNewPic = new Bitmap(strOldPic, intWidth, intHeight);
                return objNewPic;
            }
            catch (Exception exp) { throw exp; }
        }

        public Bitmap CutImage(Bitmap sourceBitmap, Rectangle rc)
        {
            if (rc.Bottom < 0)
                return null;
            Bitmap TempsourceBitmap = new Bitmap(rc.Right - rc.Left, rc.Bottom - rc.Top);
            Graphics gr = Graphics.FromImage(TempsourceBitmap);
            gr.DrawImage(sourceBitmap, 0, 0, new RectangleF(rc.Left, rc.Top, rc.Right - rc.Left, rc.Bottom - rc.Top), GraphicsUnit.Pixel);
            gr.Dispose();
            return TempsourceBitmap;
        }
        public Bitmap JoinImage(Bitmap sourceBitmap, Bitmap joinBitmap, Rectangle rc)
        {
            Bitmap TempsourceBitmap = sourceBitmap;
            Graphics gr = Graphics.FromImage(TempsourceBitmap);
            gr.DrawImage(joinBitmap, 0, 0, new RectangleF(rc.Left, rc.Top, rc.Right - rc.Left, rc.Bottom - rc.Top), GraphicsUnit.Pixel);
            gr.Dispose();
            return TempsourceBitmap;
        }
        /// <summary>
        /// 铺满
        /// </summary>
        /// <param name="sourceBitmap"></param>
        /// <param name="joinBitmap"></param>
        /// <param name="rc"></param>
        /// <returns></returns>
        public Bitmap JoinMImage(Bitmap sourceBitmap, Bitmap joinBitmap, Rectangle rc)
        {
            joinBitmap = SmallPic(joinBitmap, sourceBitmap.Width, sourceBitmap.Height);
            Bitmap TempsourceBitmap = sourceBitmap;
            Graphics gr = Graphics.FromImage(TempsourceBitmap);
            gr.DrawImage(joinBitmap, 0, 0, new RectangleF(rc.Left, rc.Top, rc.Right - rc.Left, rc.Bottom - rc.Top), GraphicsUnit.Pixel);
            gr.Dispose();
            return TempsourceBitmap;
        }

        public Bitmap JoinMImage(Bitmap sourceBitmap, Bitmap joinBitmap, Rectangle rc, int x, int y)
        {
            Bitmap TempsourceBitmap = sourceBitmap;
            Graphics gr = Graphics.FromImage(TempsourceBitmap);
            gr.DrawImage(joinBitmap, x, y, new RectangleF(rc.Left, rc.Top, rc.Right - rc.Left, rc.Bottom - rc.Top), GraphicsUnit.Pixel);
            gr.Dispose();
            return TempsourceBitmap;
        }
        public Bitmap JoinTxtImage(Bitmap sourceBitmap, string txt, Color color, int x, int y)
        {
            Bitmap TempsourceBitmap = sourceBitmap;
            Graphics gr = Graphics.FromImage(TempsourceBitmap);
            if (sourceBitmap.Width > 200)
                gr.DrawString(txt, new Font("微软雅黑", 13, FontStyle.Bold), new SolidBrush(color), new PointF(x, y));
            else
                gr.DrawString(txt, new Font("微软雅黑", 12, FontStyle.Bold), new SolidBrush(color), new PointF(x, y));
            gr.Dispose();
            return TempsourceBitmap;
        }
    }
}
失落的神庙 2014-04-29
  • 打赏
  • 举报
回复
稍后。 纳闷为什么没人给你回帖

111,092

社区成员

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

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

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