private byte[] PutLogoInImage(Image commodityImage, Image priceImage,
string activePrice, bool checked11, Image brandLogoImage = null)
{
byte[] result = null;
Image im = commodityImage;
using (Image mypriceImage = priceImage.Clone() as Image)
{
LogoPriceImage(mypriceImage, activePrice, checked11);
//创建画刷
using (Graphics g = Graphics.FromImage(im))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default;
if (brandLogoImage != null)
{
using (Bitmap bm = new Bitmap(brandLogoImage))
{
//打左上角
g.DrawImage(bm, new System.Drawing.Rectangle(0, 0, bm.Width, bm.Height), 0,
0, bm.Width, bm.Height, GraphicsUnit.Pixel);
}
}
using (Bitmap bm = new Bitmap(mypriceImage))
{
#region 缩略图
//var tempImg= bm.GetThumbnailImage(250, 250, null, new IntPtr());
//g.DrawImage(tempImg, new Rectangle(im.Width - tempImg.Width, im.Height - tempImg.Height, tempImg.Width, tempImg.Height));
#endregion
//bm.MakeTransparent(Color.White); //注释掉,否则底图会对图标有影响 zhjw 20151102
//打到右下角
g.DrawImage(bm, new Rectangle(im.Width - bm.Width, im.Height - bm.Height, bm.Width, bm.Height));
}
}
}
//将保存好的图片byte[]
using (MemoryStream ms = new MemoryStream())
{
using (Bitmap bmp = new Bitmap(im))//必须创建一个新的对象
{
var ici = GetImageEncoder(im);
if (ici == null)
{
bmp.Save(ms, im.RawFormat);
}
else
{
using (var ep = new EncoderParameters
{
Param = new EncoderParameter[] {
new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L) }
})
{
bmp.Save(ms, ici, ep);
}
}
result = ms.ToArray();
}
}
return result;
}
private static ImageCodecInfo GetImageEncoder(Image img)
{
//关键质量控制
//获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff
var icis = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo i in icis)
{
if (img.RawFormat.Guid == i.FormatID)//图片格式一致
//if (i.MimeType == "image/png") //默认取 png编码器
{
ici = i;
break;
}
}
return ici;
}
C#图片处理像素质量等代码问题
最新推荐文章于 2020-10-30 16:53:23 发布