图片加文字后质量变差的问题
用以下办法给图片加文字,原来有10k的图片变成6K了,请指教
System.Drawing.Image image = System.Drawing.Image.FromFile( "d:\\img\\error.jpg ");
Graphics g = Graphics.FromImage(image);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f = new Font( "宋体 ",3);
Brush b;
b = new SolidBrush(Color.Red);
string addText = "ABCD ";
g.DrawString(addText, f, b, 90, 170);
g.Dispose();
image.Save( "d:\\img\\okfff1.jpg ");
------解决方案--------------------帮顶!
------解决方案--------------------抢个首发!
------解决方案--------------------直接保存不是高质量的
参考下面(Stream参数可以换为路径直接保存文件):
/// <summary>
/// 图片压缩(改变质量以调整文件的大小)
/// </summary>
/// <param name= "srcBitmap "> 传入的Bitmap对象 </param>
/// <param name= "destStream "> 压缩后的Stream对象 </param>
/// <param name= "level "> 压缩等级,0到100,0 最差质量,100 最佳 </param>
public static void Compress(Bitmap srcBitmap,Stream destStream,long level)
{
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
// Get an ImageCodecInfo object that represents the JPEG codec.
myImageCodecInfo = GetEncoderInfo( "image/jpeg ");
// Create an Encoder object based on the GUID
// for the Quality parameter category.
myEncoder = Encoder.Quality;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
myEncoderParameters = new EncoderParameters(1);
// Save the bitmap as a JPEG file with 给定的 quality level
myEncoderParameter = new EncoderParameter(myEncoder, level);
myEncoderParameters.Param[0] = myEncoderParameter;
srcBitmap.Save(destStream, myImageCodecInfo, myEncoderParameters);
}
------解决方案--------------------指定一个适当的质量就可以了
------解决方案--------------------只是加字的话,以下方法即可,源文件压缩得还行.
public void AttachLogoOnBitmap(string file, string logo, int logoType, int x, int y)
{
try
{
if (file == null || file == string.Empty || logo == null || logo == string.Empty)
{
return;
}
MemoryStream s = new MemoryStream();
Image img = Image.FromFile(file);
Graphics graphics = Graphics.FromImage(img);
graphics.SmoothingMode = SmoothingMode.HighQuality;