当前位置:  开发笔记 > 编程语言 > 正文

C#为图像添加图层

如何解决《C#为图像添加图层》经验,为你挑选了1个好方法。

我想为带有公司徽标的图像添加图层.

徽标应放在图像的中心(少许不透明度).

我怎样才能做到这一点?



1> REA_ANDREW..:

这是我之前制作的一个为某些图像创建新徽章:

编辑,我设计了我提供maxWidth和maxHeight的功能,它调整大小而不失真.

要求:

using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

代码

    using (Image i = Image.FromFile(HttpContext.Current.Server.MapPath(fileName)))
    {
        float imageWidth = i.PhysicalDimension.Width;
        float imageHeight = i.PhysicalDimension.Height;
        float percentage = maxWidth / imageWidth;
        float newWidth = imageWidth * percentage;
        float newHeight = imageHeight * percentage;

        if (newHeight > maxHeight)
        {
            percentage = maxHeight / newHeight;

            newWidth = newWidth * percentage;
            newHeight = newHeight * percentage;
        }

        using (Bitmap b = new Bitmap((int)newWidth, (int)newHeight))
        {
            using (Graphics g = Graphics.FromImage(b))
            {
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

                g.DrawImage(i, new Rectangle(0, 0, b.Width, b.Height));

                if (effect == "new")
                {
                    using (Image j = Image.FromFile(HttpContext.Current.Server.MapPath("/ImageEffects/") + "new.png", true))
                    {
                        g.DrawImage(j, new Rectangle(0, 0, 60, 60));

                    }
                }

                Image newImage = Image.FromHbitmap(b.GetHbitmap());

                return newImage;
            }
        }

    }
}

推荐阅读
手机用户2502851955
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有