这个图片保存下来后上传很清晰:
http://www.efu.com.cn/PRODUCT/pic/10/00/big_597.jpg
而这个图片上传后很模糊:
http://www.dress-info.com/tupian.jpg
效果在:
http://www.dress-info.com/product/index.aspx
函数是用image.Save的
8 个解决方案
#1
你的size是怎么算的
#2
首先比例一定要一致,另外存成gif好象比jpg效果要好
#3
可以看看有关GDI+之类的书,有提供缩略图的做法的,我试了n次,缩略的图片都比较清晰
#4
是啊,我缩的也很清晰啊..可能是算法有问题吧.
#5
比例倒无所谓,
我现在电脑上没有代码,晚上我把代码贴上来看看
我现在电脑上没有代码,晚上我把代码贴上来看看
#6
System.Drawing.Image img= Image.FromStreamFile1.PostedFile.InputStream);
int Width = img.Width;
int Height = img.Height;
string fileType = File1.PostedFile.ContentType;
if(fileType!="image/gif" && fileType!="image/pjpg" && fileType!"image/pjpeg"&&fileType != "image/bmp")
{
errorMessage.Text ="产品图片只能是GIF和jpg格式";
return;
}
if(Width>3000 || Height>3000 || File1.PostedFile.ContentLength>5000*1024)
{
errorMessage.Text = "图片太大";
return;
}
int fWidth =0;
int fHeight =0;
if(Width >300 || Height >400)
{
float k = 0.0f;
if((Width/3) > (Height/4))
k = (float)Width / 300.0f;
else
k = (float)Height/400.0f;
fWidth =(int)( (float)Width/k);
fHeight =(int)((float)Height/k);
img = img.GetThumbnailImage(fWidth,fHeight,null,IntPtr.Zero);
}
{
string sdate = System.DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss_");
str_BigPhotoURL = sdate+"big_"+userName+".jpg";
str_SmallPhotoURL = sdate+"small_"+userName+".jpg";
string sBigPath =Server.MapPath("/dsProductPic/"+str_BigPhotoURL);
string sSmallPath =Server.MapPath("/dsProductPic/"+str_SmallPhotoURL);
img.Save(sBigPath,ImageFormat.Jpeg);//保存大图片
img = img.GetThumbnailImage((int)(100),(int)(130),null,IntPtr.Zero);
img.Save(sSmallPath,ImageFormat.Jpeg);//保存小图片
}
以上是源代码,就是不知道为什么?有的清晰有的模糊!
int Width = img.Width;
int Height = img.Height;
string fileType = File1.PostedFile.ContentType;
if(fileType!="image/gif" && fileType!="image/pjpg" && fileType!"image/pjpeg"&&fileType != "image/bmp")
{
errorMessage.Text ="产品图片只能是GIF和jpg格式";
return;
}
if(Width>3000 || Height>3000 || File1.PostedFile.ContentLength>5000*1024)
{
errorMessage.Text = "图片太大";
return;
}
int fWidth =0;
int fHeight =0;
if(Width >300 || Height >400)
{
float k = 0.0f;
if((Width/3) > (Height/4))
k = (float)Width / 300.0f;
else
k = (float)Height/400.0f;
fWidth =(int)( (float)Width/k);
fHeight =(int)((float)Height/k);
img = img.GetThumbnailImage(fWidth,fHeight,null,IntPtr.Zero);
}
{
string sdate = System.DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss_");
str_BigPhotoURL = sdate+"big_"+userName+".jpg";
str_SmallPhotoURL = sdate+"small_"+userName+".jpg";
string sBigPath =Server.MapPath("/dsProductPic/"+str_BigPhotoURL);
string sSmallPath =Server.MapPath("/dsProductPic/"+str_SmallPhotoURL);
img.Save(sBigPath,ImageFormat.Jpeg);//保存大图片
img = img.GetThumbnailImage((int)(100),(int)(130),null,IntPtr.Zero);
img.Save(sSmallPath,ImageFormat.Jpeg);//保存小图片
}
以上是源代码,就是不知道为什么?有的清晰有的模糊!
#7
/// <summary>
/// 为传入的图像生成缩略图
/// </summary>
/// <param name="sourceImage">传入的图像</param>
/// <param name="widthOfThumbnailImage">缩略图的宽度(像素)</param>
/// <returns>System.Drawing.Image类型的缩略图</returns>
public static System.Drawing.Image GetThumbnailImage(System.Drawing.Image sourceImage,int widthOfThumbnailImage)
{
System.Drawing.Image imgThumb;
int width = sourceImage.Width;
int height = sourceImage.Height;
int heightOfThumbnailImage;
float rate = (float)widthOfThumbnailImage/width;
if(rate>=1)
{
widthOfThumbnailImage = width;
heightOfThumbnailImage = height;
}
else
{
heightOfThumbnailImage = (int) (rate * height);
}
imgThumb = new System.Drawing.Bitmap(widthOfThumbnailImage, heightOfThumbnailImage);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgThumb);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(sourceImage, new Rectangle(0, 0, widthOfThumbnailImage, heightOfThumbnailImage), 0, 0, width, height, GraphicsUnit.Pixel);
return imgThumb;
}
/// <summary>
/// 为传入的图像生成缩略图
/// </summary>
/// <param name="sourceImageStream">传入的图像数据流</param>
/// <param name="widthOfThumbnailImage">缩略图的宽度(像素)</param>
/// <returns>byte[]类型的缩略图</returns>
public static byte[] GetThumbnailImage(Stream sourceImageStream,int widthOfThumbnailImage)
{
byte[] Thumb;
sourceImageStream.Seek(0,0);
System.Drawing.Image imgSource = new System.Drawing.Bitmap(sourceImageStream);
System.Drawing.Image imgThumb = GetThumbnailImage(imgSource,widthOfThumbnailImage);
Stream ThumbnailImageStream = new System.IO.MemoryStream();
imgThumb.Save(ThumbnailImageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
long ThumbnailImageFileLength = ThumbnailImageStream.Length;
Thumb = new byte[ThumbnailImageFileLength];
ThumbnailImageStream.Seek(0,0);
ThumbnailImageStream.Read(Thumb,0,(int)ThumbnailImageFileLength);
ThumbnailImageStream.Close();
return Thumb;
}
/// 为传入的图像生成缩略图
/// </summary>
/// <param name="sourceImage">传入的图像</param>
/// <param name="widthOfThumbnailImage">缩略图的宽度(像素)</param>
/// <returns>System.Drawing.Image类型的缩略图</returns>
public static System.Drawing.Image GetThumbnailImage(System.Drawing.Image sourceImage,int widthOfThumbnailImage)
{
System.Drawing.Image imgThumb;
int width = sourceImage.Width;
int height = sourceImage.Height;
int heightOfThumbnailImage;
float rate = (float)widthOfThumbnailImage/width;
if(rate>=1)
{
widthOfThumbnailImage = width;
heightOfThumbnailImage = height;
}
else
{
heightOfThumbnailImage = (int) (rate * height);
}
imgThumb = new System.Drawing.Bitmap(widthOfThumbnailImage, heightOfThumbnailImage);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgThumb);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(sourceImage, new Rectangle(0, 0, widthOfThumbnailImage, heightOfThumbnailImage), 0, 0, width, height, GraphicsUnit.Pixel);
return imgThumb;
}
/// <summary>
/// 为传入的图像生成缩略图
/// </summary>
/// <param name="sourceImageStream">传入的图像数据流</param>
/// <param name="widthOfThumbnailImage">缩略图的宽度(像素)</param>
/// <returns>byte[]类型的缩略图</returns>
public static byte[] GetThumbnailImage(Stream sourceImageStream,int widthOfThumbnailImage)
{
byte[] Thumb;
sourceImageStream.Seek(0,0);
System.Drawing.Image imgSource = new System.Drawing.Bitmap(sourceImageStream);
System.Drawing.Image imgThumb = GetThumbnailImage(imgSource,widthOfThumbnailImage);
Stream ThumbnailImageStream = new System.IO.MemoryStream();
imgThumb.Save(ThumbnailImageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
long ThumbnailImageFileLength = ThumbnailImageStream.Length;
Thumb = new byte[ThumbnailImageFileLength];
ThumbnailImageStream.Seek(0,0);
ThumbnailImageStream.Read(Thumb,0,(int)ThumbnailImageFileLength);
ThumbnailImageStream.Close();
return Thumb;
}
#8
学习
#1
你的size是怎么算的
#2
首先比例一定要一致,另外存成gif好象比jpg效果要好
#3
可以看看有关GDI+之类的书,有提供缩略图的做法的,我试了n次,缩略的图片都比较清晰
#4
是啊,我缩的也很清晰啊..可能是算法有问题吧.
#5
比例倒无所谓,
我现在电脑上没有代码,晚上我把代码贴上来看看
我现在电脑上没有代码,晚上我把代码贴上来看看
#6
System.Drawing.Image img= Image.FromStreamFile1.PostedFile.InputStream);
int Width = img.Width;
int Height = img.Height;
string fileType = File1.PostedFile.ContentType;
if(fileType!="image/gif" && fileType!="image/pjpg" && fileType!"image/pjpeg"&&fileType != "image/bmp")
{
errorMessage.Text ="产品图片只能是GIF和jpg格式";
return;
}
if(Width>3000 || Height>3000 || File1.PostedFile.ContentLength>5000*1024)
{
errorMessage.Text = "图片太大";
return;
}
int fWidth =0;
int fHeight =0;
if(Width >300 || Height >400)
{
float k = 0.0f;
if((Width/3) > (Height/4))
k = (float)Width / 300.0f;
else
k = (float)Height/400.0f;
fWidth =(int)( (float)Width/k);
fHeight =(int)((float)Height/k);
img = img.GetThumbnailImage(fWidth,fHeight,null,IntPtr.Zero);
}
{
string sdate = System.DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss_");
str_BigPhotoURL = sdate+"big_"+userName+".jpg";
str_SmallPhotoURL = sdate+"small_"+userName+".jpg";
string sBigPath =Server.MapPath("/dsProductPic/"+str_BigPhotoURL);
string sSmallPath =Server.MapPath("/dsProductPic/"+str_SmallPhotoURL);
img.Save(sBigPath,ImageFormat.Jpeg);//保存大图片
img = img.GetThumbnailImage((int)(100),(int)(130),null,IntPtr.Zero);
img.Save(sSmallPath,ImageFormat.Jpeg);//保存小图片
}
以上是源代码,就是不知道为什么?有的清晰有的模糊!
int Width = img.Width;
int Height = img.Height;
string fileType = File1.PostedFile.ContentType;
if(fileType!="image/gif" && fileType!="image/pjpg" && fileType!"image/pjpeg"&&fileType != "image/bmp")
{
errorMessage.Text ="产品图片只能是GIF和jpg格式";
return;
}
if(Width>3000 || Height>3000 || File1.PostedFile.ContentLength>5000*1024)
{
errorMessage.Text = "图片太大";
return;
}
int fWidth =0;
int fHeight =0;
if(Width >300 || Height >400)
{
float k = 0.0f;
if((Width/3) > (Height/4))
k = (float)Width / 300.0f;
else
k = (float)Height/400.0f;
fWidth =(int)( (float)Width/k);
fHeight =(int)((float)Height/k);
img = img.GetThumbnailImage(fWidth,fHeight,null,IntPtr.Zero);
}
{
string sdate = System.DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss_");
str_BigPhotoURL = sdate+"big_"+userName+".jpg";
str_SmallPhotoURL = sdate+"small_"+userName+".jpg";
string sBigPath =Server.MapPath("/dsProductPic/"+str_BigPhotoURL);
string sSmallPath =Server.MapPath("/dsProductPic/"+str_SmallPhotoURL);
img.Save(sBigPath,ImageFormat.Jpeg);//保存大图片
img = img.GetThumbnailImage((int)(100),(int)(130),null,IntPtr.Zero);
img.Save(sSmallPath,ImageFormat.Jpeg);//保存小图片
}
以上是源代码,就是不知道为什么?有的清晰有的模糊!
#7
/// <summary>
/// 为传入的图像生成缩略图
/// </summary>
/// <param name="sourceImage">传入的图像</param>
/// <param name="widthOfThumbnailImage">缩略图的宽度(像素)</param>
/// <returns>System.Drawing.Image类型的缩略图</returns>
public static System.Drawing.Image GetThumbnailImage(System.Drawing.Image sourceImage,int widthOfThumbnailImage)
{
System.Drawing.Image imgThumb;
int width = sourceImage.Width;
int height = sourceImage.Height;
int heightOfThumbnailImage;
float rate = (float)widthOfThumbnailImage/width;
if(rate>=1)
{
widthOfThumbnailImage = width;
heightOfThumbnailImage = height;
}
else
{
heightOfThumbnailImage = (int) (rate * height);
}
imgThumb = new System.Drawing.Bitmap(widthOfThumbnailImage, heightOfThumbnailImage);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgThumb);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(sourceImage, new Rectangle(0, 0, widthOfThumbnailImage, heightOfThumbnailImage), 0, 0, width, height, GraphicsUnit.Pixel);
return imgThumb;
}
/// <summary>
/// 为传入的图像生成缩略图
/// </summary>
/// <param name="sourceImageStream">传入的图像数据流</param>
/// <param name="widthOfThumbnailImage">缩略图的宽度(像素)</param>
/// <returns>byte[]类型的缩略图</returns>
public static byte[] GetThumbnailImage(Stream sourceImageStream,int widthOfThumbnailImage)
{
byte[] Thumb;
sourceImageStream.Seek(0,0);
System.Drawing.Image imgSource = new System.Drawing.Bitmap(sourceImageStream);
System.Drawing.Image imgThumb = GetThumbnailImage(imgSource,widthOfThumbnailImage);
Stream ThumbnailImageStream = new System.IO.MemoryStream();
imgThumb.Save(ThumbnailImageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
long ThumbnailImageFileLength = ThumbnailImageStream.Length;
Thumb = new byte[ThumbnailImageFileLength];
ThumbnailImageStream.Seek(0,0);
ThumbnailImageStream.Read(Thumb,0,(int)ThumbnailImageFileLength);
ThumbnailImageStream.Close();
return Thumb;
}
/// 为传入的图像生成缩略图
/// </summary>
/// <param name="sourceImage">传入的图像</param>
/// <param name="widthOfThumbnailImage">缩略图的宽度(像素)</param>
/// <returns>System.Drawing.Image类型的缩略图</returns>
public static System.Drawing.Image GetThumbnailImage(System.Drawing.Image sourceImage,int widthOfThumbnailImage)
{
System.Drawing.Image imgThumb;
int width = sourceImage.Width;
int height = sourceImage.Height;
int heightOfThumbnailImage;
float rate = (float)widthOfThumbnailImage/width;
if(rate>=1)
{
widthOfThumbnailImage = width;
heightOfThumbnailImage = height;
}
else
{
heightOfThumbnailImage = (int) (rate * height);
}
imgThumb = new System.Drawing.Bitmap(widthOfThumbnailImage, heightOfThumbnailImage);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgThumb);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(sourceImage, new Rectangle(0, 0, widthOfThumbnailImage, heightOfThumbnailImage), 0, 0, width, height, GraphicsUnit.Pixel);
return imgThumb;
}
/// <summary>
/// 为传入的图像生成缩略图
/// </summary>
/// <param name="sourceImageStream">传入的图像数据流</param>
/// <param name="widthOfThumbnailImage">缩略图的宽度(像素)</param>
/// <returns>byte[]类型的缩略图</returns>
public static byte[] GetThumbnailImage(Stream sourceImageStream,int widthOfThumbnailImage)
{
byte[] Thumb;
sourceImageStream.Seek(0,0);
System.Drawing.Image imgSource = new System.Drawing.Bitmap(sourceImageStream);
System.Drawing.Image imgThumb = GetThumbnailImage(imgSource,widthOfThumbnailImage);
Stream ThumbnailImageStream = new System.IO.MemoryStream();
imgThumb.Save(ThumbnailImageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
long ThumbnailImageFileLength = ThumbnailImageStream.Length;
Thumb = new byte[ThumbnailImageFileLength];
ThumbnailImageStream.Seek(0,0);
ThumbnailImageStream.Read(Thumb,0,(int)ThumbnailImageFileLength);
ThumbnailImageStream.Close();
return Thumb;
}
#8
学习