在某些情况下希望处理一些图片,比如
给图片添加一般文字:
原图
程序处理成
给图片添加水印:
原图
程序处理成
还有圆角效果:
原图
程序处理成
注:
(1)以下程序针对这个应用,也许这些对你没有用,但是基本的原理都差不多
(2)以上图为测试用,应该不算侵犯肖像权吧
程序界面:
程序点击这里下载。
如果需要提高图片质量,把
image.Save(sDstFilePath,ImageFormat.Jpeg);
改成
ImageCodecInfomyImageCodecInfo;
EncodermyEncoder;
EncoderParametermyEncoderParameter;
EncoderParametersmyEncoderParameters;
myImageCodecInfo
=
ImageCodecInfo.GetImageEncoders()[
0
];
myEncoder
=
Encoder.Quality;
myEncoderParameters
=
new
EncoderParameters(
1
);
myEncoderParameter
=
new
EncoderParameter(myEncoder,1
00L
); // 0-100
myEncoderParameters.Param[
0
]
=
myEncoderParameter;
image.Save(sDstFilePath,myImageCodecInfo,myEncoderParameters);
myEncoderParameter.Dispose();
myEncoderParameters.Dispose();
这是提高质量后的效果:
附一些关键代码:
publicclassMyGDI
{
publicstaticvoidCreateWatermark(stringsSrcFilePath,stringsDstFilePath,stringsText1,stringsColor1,stringsSize1,stringsFont1,stringsText2,stringsColor2,stringsSize2,stringsFont2,stringsBgColor,stringsTransparence)
{
System.Drawing.Imageimage=System.Drawing.Image.FromFile(sSrcFilePath);
Graphicsg=Graphics.FromImage(image);
g.SmoothingMode=SmoothingMode.AntiAlias;
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
g.CompositingQuality=CompositingQuality.HighQuality;
g.TextRenderingHint=System.Drawing.Text.TextRenderingHint.AntiAlias;//文字抗锯齿
g.DrawImage(image,0,0,image.Width,image.Height);
Fontf1=newFont(sFont1,float.Parse(sSize1));
Fontf2=newFont(sFont2,float.Parse(sSize2));
Brushbrushfortext1=newSolidBrush(ColorTranslator.FromHtml(sColor1));
Brushbrushfortext2=newSolidBrush(ColorTranslator.FromHtml(sColor2));
Brushbrushforbg=newSolidBrush(Color.FromArgb(Convert.ToInt16(255*float.Parse(sTransparence)),ColorTranslator.FromHtml(sBgColor)));
g.RotateTransform(-20);
Rectanglerect=newRectangle(-image.Width/2-50,image.Height-50,image.Width*2,40);
g.DrawRectangle(newPen(brushforbg),rect);
g.FillRectangle(brushforbg,rect);
Rectanglerectfortext1=newRectangle(-image.Width/2+image.Width/5,image.Height-45,image.Width*2,60);
for(inti=0;i<10;i++)
g.DrawString(sText1,f1,brushfortext1,rectfortext1);
Rectanglerectfortext2=newRectangle(-image.Width/2+image.Width/5,image.Height-25,image.Width*2,60);
for(inti=0;i<10;i++)
g.DrawString(sText2,f2,brushfortext2,rectfortext2);
image.Save(sDstFilePath,ImageFormat.Jpeg);
image.Dispose();
}
publicstaticvoidCreateRoundedCorner(stringsSrcFilePath,stringsDstFilePath,stringsCornerLocation)
{
System.Drawing.Imageimage=System.Drawing.Image.FromFile(sSrcFilePath);
Graphicsg=Graphics.FromImage(image);
g.SmoothingMode=SmoothingMode.HighQuality;
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
g.CompositingQuality=CompositingQuality.HighQuality;
Rectanglerect=newRectangle(0,0,image.Width,image.Height);
GraphicsPathrectPath=CreateRoundRectanglePath(rect,image.Width/10,sCornerLocation);//构建圆角外部路径
Brushb=newSolidBrush(Color.White);//圆角背景白色
g.DrawPath(newPen(b),rectPath);
g.FillPath(b,rectPath);
g.Dispose();
image.Save(sDstFilePath,ImageFormat.Jpeg);
image.Dispose();
}
publicstaticvoidCreatePlainText(stringsSrcFilePath,stringsDstFilePath,stringsText,stringsColor,stringsSize,stringsFont)
{
System.Drawing.Imageimage=System.Drawing.Image.FromFile(sSrcFilePath);
Graphicsg=Graphics.FromImage(image);
g.SmoothingMode=SmoothingMode.AntiAlias;
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
g.CompositingQuality=CompositingQuality.HighQuality;
g.DrawImage(image,0,0,image.Width,image.Height);
g.TextRenderingHint=System.Drawing.Text.TextRenderingHint.AntiAlias;//文字抗锯齿
Fontf=newFont(sFont,float.Parse(sSize));
Brushb=newSolidBrush(ColorTranslator.FromHtml(sColor));
Rectanglerect=newRectangle(10,5,image.Width,image.Height);//适当空开一段距离
for(inti=0;i<30;i++)//加强亮度
g.DrawString(sText,f,b,rect);
image.Save(sDstFilePath,ImageFormat.Jpeg);
image.Dispose();
}
privatestaticGraphicsPathCreateRoundRectanglePath(Rectanglerect,intradius,stringsPosition)
{
GraphicsPathrectPath=newGraphicsPath();
switch(sPosition)
{
case"TopLeft":
{
rectPath.AddArc(rect.Left,rect.Top,radius*2,radius*2,180,90);
rectPath.AddLine(rect.Left,rect.Top,rect.Left,rect.Top+radius);
break;
}
case"TopRight":
{
rectPath.AddArc(rect.Right-radius*2,rect.Top,radius*2,radius*2,270,90);
rectPath.AddLine(rect.Right,rect.Top,rect.Right-radius,rect.Top);
break;
}
case"BottomLeft":
{
rectPath.AddArc(rect.Left,rect.Bottom-radius*2,radius*2,radius*2,90,90);
rectPath.AddLine(rect.Left,rect.Bottom-radius,rect.Left,rect.Bottom);
break;
}
case"BottomRight":
{
rectPath.AddArc(rect.Right-radius*2,rect.Bottom-radius*2,radius*2,radius*2,0,90);
rectPath.AddLine(rect.Right-radius,rect.Bottom,rect.Right,rect.Bottom);
break;
}
}
returnrectPath;
}
}