C#为图像添加图层 [英] C# add a layer to an image
本文介绍了C#为图像添加图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在图片上添加一个带有公司徽标的图层。
I would like to add a layer to an image with the logo of the company.
徽标应放在图像的中心(小不透明度) 。
The logo should be placed on the center of the image (little opacity).
我该怎么做?
推荐答案
这是一个我之前做的是为某些图像创建了一个新徽章:
Here is one I made earlier which creates a new badge for some images:
编辑,我设计了我提供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;
}
}
}
}
这篇关于C#为图像添加图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文