图片通过程序压缩大小清晰度下降,颜色变浅了点
然后自动加水印处理,清晰度也下降了,压缩代码如下。高手指点压缩代码如下
/// <summary>
/// 根据原图生成缩略图
/// </summary>
/// <param name="originalImagePath">原图象地址</param>
/// <param name="thumbnailPath">生成的图像地址</param>
/// <param name="width">生成图像的长度</param>
/// <param name="height">生成图像的宽度</param>
/// <param name="mode">模式</param>
public static void MakeSmallPic(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage;
try
{
originalImage = System.Drawing.Image.FromFile(originalImagePath);
}
catch
{
//File.Delete(originalImagePath);
return;
}
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "MHW"://指定最大高宽缩放不变行
if (originalImage.Width <= width && originalImage.Height <= height)
{
towidth = originalImage.Width;
toheight = originalImage.Height;
}
else
{
if (originalImage.Width > width)
{
towidth = width;
toheight = originalImage.Height * towidth / originalImage.Width;
}
if (toheight>height)
{
towidth = towidth * height / toheight;
toheight = height;
}
}
break;
case "W": //指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H": //指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut": //指定高宽裁减(不变形)
if ((originalImage.Width / originalImage.Height) > (towidth / toheight))
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break; }
// 新建一个bmp图片
System.Drawing.Image bitmap;
bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
System.Drawing.Graphics g;
g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
// bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
//写数据库
EncoderParameter p;
EncoderParameters ps;
ps = new EncoderParameters(1);
p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
ps.Param[0] = p;
ImageCodecInfo ii = GetCodecInfo(getMimeType(originalImagePath));
bitmap.Save(thumbnailPath, ii, ps);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
private static ImageCodecInfo GetCodecInfo(string mimeType)
{
ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo ici in CodecInfo)
{
if (ici.MimeType == mimeType) return ici;
}
return null;
}
public static string getMimeType(string filename)
{
string ext= filename.Substring(filename.LastIndexOf(".") + 1, (filename.Length - filename.LastIndexOf(".") - 1));
string miniType = "image/jpeg";
switch (ext)
{
case ".jpg":
miniType = "image/jpeg";
break;
case ".gif":
miniType = "image/gif";
break;
case ".png":
miniType = "image/png";
break;
}
return miniType;
}加水印代码如下
string imagePath = "原图地址";
string waterMarkPath = "~/water.png"; //水印地址
Image image = null;
image = Image.FromFile(context.Server.MapPath(imagePath));
Image waterMark = Image.FromFile(context.Server.MapPath(waterMarkPath));
Graphics g = Graphics.FromImage(image);
g.DrawImage(waterMark, image.Width/2 - waterMark.Width/2,
image.Height/2 - waterMark.Height/2, waterMark.Width, waterMark.Height); waterMark.Dispose();
g.Dispose();
HttpResponse response = context.Response;
response.Clear();
//输出流的类型为图片
response.ContentType = "image/jpeg"; EncoderParameter p;
EncoderParameters ps;
ps = new EncoderParameters(1);
p = new EncoderParameter(Encoder.Quality, 100L);
ps.Param[0] = p;
ImageCodecInfo ii = GetCodecInfo(getMimeType(imagePath));
image.Save(response.OutputStream,ii,ps);
response.End();
image.Dispose();
然后自动加水印处理,清晰度也下降了,压缩代码如下。高手指点压缩代码如下
/// <summary>
/// 根据原图生成缩略图
/// </summary>
/// <param name="originalImagePath">原图象地址</param>
/// <param name="thumbnailPath">生成的图像地址</param>
/// <param name="width">生成图像的长度</param>
/// <param name="height">生成图像的宽度</param>
/// <param name="mode">模式</param>
public static void MakeSmallPic(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage;
try
{
originalImage = System.Drawing.Image.FromFile(originalImagePath);
}
catch
{
//File.Delete(originalImagePath);
return;
}
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "MHW"://指定最大高宽缩放不变行
if (originalImage.Width <= width && originalImage.Height <= height)
{
towidth = originalImage.Width;
toheight = originalImage.Height;
}
else
{
if (originalImage.Width > width)
{
towidth = width;
toheight = originalImage.Height * towidth / originalImage.Width;
}
if (toheight>height)
{
towidth = towidth * height / toheight;
toheight = height;
}
}
break;
case "W": //指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H": //指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut": //指定高宽裁减(不变形)
if ((originalImage.Width / originalImage.Height) > (towidth / toheight))
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break; }
// 新建一个bmp图片
System.Drawing.Image bitmap;
bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
System.Drawing.Graphics g;
g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
// bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
//写数据库
EncoderParameter p;
EncoderParameters ps;
ps = new EncoderParameters(1);
p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
ps.Param[0] = p;
ImageCodecInfo ii = GetCodecInfo(getMimeType(originalImagePath));
bitmap.Save(thumbnailPath, ii, ps);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
private static ImageCodecInfo GetCodecInfo(string mimeType)
{
ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo ici in CodecInfo)
{
if (ici.MimeType == mimeType) return ici;
}
return null;
}
public static string getMimeType(string filename)
{
string ext= filename.Substring(filename.LastIndexOf(".") + 1, (filename.Length - filename.LastIndexOf(".") - 1));
string miniType = "image/jpeg";
switch (ext)
{
case ".jpg":
miniType = "image/jpeg";
break;
case ".gif":
miniType = "image/gif";
break;
case ".png":
miniType = "image/png";
break;
}
return miniType;
}加水印代码如下
string imagePath = "原图地址";
string waterMarkPath = "~/water.png"; //水印地址
Image image = null;
image = Image.FromFile(context.Server.MapPath(imagePath));
Image waterMark = Image.FromFile(context.Server.MapPath(waterMarkPath));
Graphics g = Graphics.FromImage(image);
g.DrawImage(waterMark, image.Width/2 - waterMark.Width/2,
image.Height/2 - waterMark.Height/2, waterMark.Width, waterMark.Height); waterMark.Dispose();
g.Dispose();
HttpResponse response = context.Response;
response.Clear();
//输出流的类型为图片
response.ContentType = "image/jpeg"; EncoderParameter p;
EncoderParameters ps;
ps = new EncoderParameters(1);
p = new EncoderParameter(Encoder.Quality, 100L);
ps.Param[0] = p;
ImageCodecInfo ii = GetCodecInfo(getMimeType(imagePath));
image.Save(response.OutputStream,ii,ps);
response.End();
image.Dispose();
解决方案 »
- 关键字:Model 程序集引用 分层开发
- 无法将类型为“System.Data.DataRowView”的对象强制转换为类型“QualityControl.Model.T_ROL_Regulatory”
- 看下着 dropdownlist 的处理
- VisualStudio.Net学习交流群
- “ 在向服务器发送请求时发生传输级错误。 (provider: 共享内存提供程序, error: 0 - 系统无法打开文件。) ”错误发生
- 为什么cookie 在服务器不响应?
- 各位帮我看看,这个网站的控件是怎么样的授权方式
- ##--如何比较两个日期--##快在线ING
- **********高手帮忙**************
- 如何将excel文件资料导入数据库
- 请问一下 MVC 中 ,我要拼接一个 id 如何拼接呢。
- 求助大大们:我想在窗体上添加几个按钮,程序运行没问题,但是加载窗体设计器的时候出现问题
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
Image originalImage = Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height; switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width/originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height/originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height*towidth/toheight;
y = 0;
x = (originalImage.Width - ow)/2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width*height/towidth;
x = 0;
y = (originalImage.Height - oh)/2;
}
break;
default :
break;
}
//新建一个bmp图片
Image bitmap = new System.Drawing.Bitmap(towidth,toheight); //新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow,oh),
GraphicsUnit.Pixel); try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
除了使用NET内置的压缩外
有没有其他优秀的第三方组件
可以实现更好的效果