当前位置 : 主页 > 网络编程 > net编程 >

asp.net 高质量缩略图

来源:互联网 收集:自由互联 发布时间:2023-10-08
2012-7-12: protected void Page_Load(object sender, EventArgs e) { string OriginalImgPath = @"C:\Users\LHTC\Desktop\ttttimg\ttttimg\imgtt\tt.jpg"; // string savepath = Path.ChangeExtension(OriginalImgPath, "jpg"); string savepath = @"C:\Us
2012-7-12:


protected void Page_Load(object sender, EventArgs e)
{
string OriginalImgPath = @"C:\Users\LHTC\Desktop\ttttimg\ttttimg\imgtt\tt.jpg";
// string savepath = Path.ChangeExtension(OriginalImgPath, "jpg");
string savepath = @"C:\Users\LHTC\Desktop\ttttimg\ttttimg\imgtt\tt1.jpg";
System.Drawing.Image image = System.Drawing.Image.FromFile(OriginalImgPath);


//System.Web.HttpPostedFile postedFile=Request.Files[0];
//System.Drawing.Image originalImage = System.Drawing.Image.FromStream(postedFile.InputStream, true);


SaveJpg(image, savepath, 157, 84, 75L);
Response.Write("ok");
}


/// <summary>
/// 自适应按比例 上传保存
/// </summary>
/// <param name="OriginalImgPath">上传图片路径</param>
/// <param name="SavePath">保存路径</param>
/// <param name="Width">缩略图宽</param>
/// <param name="Height">缩略图高</param>
/// <param name="Quality">质量 75L</param>
private static void SaveJpg(System.Drawing.Image OriginalImage, string SavePath, int Width, int Height, long Quality)
{
var rotatedImage = OriginalImage.Clone() as System.Drawing.Image;
Size thumbnailSize = GetThumbnailSize(rotatedImage, Width, Height);
Bitmap newImage = new Bitmap(thumbnailSize.Width, thumbnailSize.Height, PixelFormat.Format24bppRgb);
newImage.SetResolution(72, 72);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.Clear(Color.Transparent);
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.CompositingQuality = CompositingQuality.HighQuality;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
using (var attribute = new ImageAttributes())
{
attribute.SetWrapMode(WrapMode.TileFlipXY);
gr.DrawImage(OriginalImage, new Rectangle(new Point(0, 0), thumbnailSize), 0, 0, OriginalImage.Width, OriginalImage.Height, GraphicsUnit.Pixel, attribute);
}
}
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
Encoder myEncoder = Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, Quality);
myEncoderParameters.Param[0] = myEncoderParameter;
newImage.Save(SavePath, myImageCodecInfo, myEncoderParameters);
}




//根据长宽自适应 按原图比例缩放
private static Size GetThumbnailSize(System.Drawing.Image original, int desiredWidth, int desiredHeight)
{
var widthScale = (double)desiredWidth / original.Width;
var heightScale = (double)desiredHeight / original.Height;
var scale = widthScale < heightScale ? widthScale : heightScale;
return new Size
{
Width = (int)(scale * original.Width),
Height = (int)(scale * original.Height)
};
}
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
public static void imgSmo(string strPath, string strName, string strType, int i)
{
try
{
string path = strPath + strName + strType;
System.Drawing.Image oImage = System.Drawing.Image.FromFile(path);

Size imageSize = GetImageSize(oImage,160,70);
System.Drawing.Imaging.ImageFormat thisFormat = oImage.RawFormat;


int oWidth = oImage.Width; //原图宽度
int oHeight = oImage.Height; //原图高度

Bitmap tImage = new Bitmap(imageSize.Width, imageSize.Height);
Graphics g = Graphics.FromImage(tImage);

//质量
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

g.Clear(Color.Transparent);
g.DrawImage(oImage, new Rectangle(0, 0, imageSize.Width, imageSize.Height), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
g.Dispose();

//JPEG
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long[] quality = new long[1];
quality[0] = 75;
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;

System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];
break;
}
}


string newPath = strPath + strName + i + strType;

if (jpegICI != null)
{
tImage.Save(newPath, jpegICI, encoderParams);
}
else
{
tImage.Save(newPath, thisFormat);
}


oImage.Dispose();
tImage.Dispose();

}
catch (Exception ex)
{
throw new Exception(ex.Message);
}

}





public static Size GetImageSize(System.Drawing.Image picture,int width,int height)
{
Size imageSize=new Size(width,height);//初始化


//按比例缩小放大

double heightRatio = (double)picture.Height / picture.Width;
double widthRatio = (double)picture.Width / picture.Height;

int desiredHeight = imageSize.Height;
int desiredWidth = imageSize.Width;

imageSize.Height = desiredHeight;
if (widthRatio > 0)
imageSize.Width = Convert.ToInt32(imageSize.Height * widthRatio);

if (imageSize.Width > desiredWidth)
{
imageSize.Width = desiredWidth;
imageSize.Height = Convert.ToInt32(imageSize.Width * heightRatio);
}

return imageSize;
}






此颜色量化类库的使用,也非常简单:

using (Bitmap bmp = new Bitmap(...))
{
OctreeQuantizer quantizer = new OctreeQuantizer ( 255 , 8 ) ;
//两个参数为颜色值,和颜色深度
using ( Bitmap quantized = quantizer.Quantize ( image ) )
{
Response.ContentType = "image/gif" ;
quantized.Save ( Response.OutputStream , ImageFormat.Gif ) ;
}
}




上一篇:c# 图片插入Excel
下一篇:没有了
网友评论