C#/ASP.NET中怎么把已上传到文件夹里的正常图片,转换成缩略图,并显示到image控件中。(不失真)
1个回答
展开全部
例如你的上传控件ID是fileupload1。
过程是先保存原图在服务器。用一个生成缩略图的类生成新图同样保存在服务器。image控件的图片绑定为跟客户端的<img />是一样的。
先fileupload1.postfile.saveas(server.Mappath("~\images\img1.jpg"));
调用下边的类。返回新图路径。接下来显示在页面中。不会就M我。
public const string TempDir = "/TempUpload/";
public const string ImageDir = "/upload/Img/";
private static bool ThumbnailCallback()
{
return false;
}
/// <param name="SavePath">原始图片相对路径</param>
/// <param name="picFilePath">保存的文件名</param>
/// <param name="width">宽</param>
/// <param name="height">高</param>
public static void GetThumbnailImage(string SavePath, string picFilePath, int width, int height)
{
//添加small100的前缀大小
//程序内相对的服务器路径小图片
string SmallPath100 = HttpContext.Current.Server.MapPath(SavePath);
string machpath = HttpContext.Current.Server.MapPath(picFilePath);
string isDir = SmallPath100.Substring(0, SmallPath100.LastIndexOf('\\'));
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(isDir);
if (!di.Exists)
{
di.Create();
}
Bitmap img = new Bitmap(machpath); //read picture to memory
int h = img.Height;
int w = img.Width;
int ss, os;// source side and objective side
double temp1, temp2;
//compute the picture's proportion
temp1 = (h * 1.0D) / height;
temp2 = (w * 1.0D) / width;
if (temp1 < temp2)
{
ss = w;
os = width;
}
else
{
ss = h;
os = height;
}
double per = (os * 1.0D) / ss;
if (per < 1.0D)
{
h = (int)(h * per);
w = (int)(w * per);
}
// create the thumbnail image
System.Drawing.Image imag2 = img.GetThumbnailImage(w, h,
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback),
IntPtr.Zero);
Bitmap tempBitmap = new Bitmap(w, h);
System.Drawing.Image tempImg = System.Drawing.Image.FromHbitmap(tempBitmap.GetHbitmap());
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(tempImg);
g.Clear(Color.White);
int x, y;
x = (tempImg.Width - imag2.Width) / 2;
y = (tempImg.Height - imag2.Height) / 2;
g.DrawImage(imag2, x, y, imag2.Width, imag2.Height);
try
{
if (img != null)
img.Dispose();
if (imag2 != null)
imag2.Dispose();
if (tempBitmap != null)
tempBitmap.Dispose();
string fileExtension = System.IO.Path.GetExtension(machpath).ToLower();
//按原图片类型保存缩略图片,不按原格式图片会出现模糊,锯齿等问题.
switch (fileExtension)
{
case ".gif": tempImg.Save(SmallPath100, ImageFormat.Gif); break;
case ".jpg": tempImg.Save(SmallPath100, ImageFormat.Jpeg); break;
case ".bmp": tempImg.Save(SmallPath100, ImageFormat.Bmp); break;
case ".png": tempImg.Save(SmallPath100, ImageFormat.Png); break;
}
}
catch
{
throw new Exception("图片上传失败");
}
finally
{
//释放内存
if (tempImg != null)
tempImg.Dispose();
if (g != null)
g.Dispose();
}
}
过程是先保存原图在服务器。用一个生成缩略图的类生成新图同样保存在服务器。image控件的图片绑定为跟客户端的<img />是一样的。
先fileupload1.postfile.saveas(server.Mappath("~\images\img1.jpg"));
调用下边的类。返回新图路径。接下来显示在页面中。不会就M我。
public const string TempDir = "/TempUpload/";
public const string ImageDir = "/upload/Img/";
private static bool ThumbnailCallback()
{
return false;
}
/// <param name="SavePath">原始图片相对路径</param>
/// <param name="picFilePath">保存的文件名</param>
/// <param name="width">宽</param>
/// <param name="height">高</param>
public static void GetThumbnailImage(string SavePath, string picFilePath, int width, int height)
{
//添加small100的前缀大小
//程序内相对的服务器路径小图片
string SmallPath100 = HttpContext.Current.Server.MapPath(SavePath);
string machpath = HttpContext.Current.Server.MapPath(picFilePath);
string isDir = SmallPath100.Substring(0, SmallPath100.LastIndexOf('\\'));
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(isDir);
if (!di.Exists)
{
di.Create();
}
Bitmap img = new Bitmap(machpath); //read picture to memory
int h = img.Height;
int w = img.Width;
int ss, os;// source side and objective side
double temp1, temp2;
//compute the picture's proportion
temp1 = (h * 1.0D) / height;
temp2 = (w * 1.0D) / width;
if (temp1 < temp2)
{
ss = w;
os = width;
}
else
{
ss = h;
os = height;
}
double per = (os * 1.0D) / ss;
if (per < 1.0D)
{
h = (int)(h * per);
w = (int)(w * per);
}
// create the thumbnail image
System.Drawing.Image imag2 = img.GetThumbnailImage(w, h,
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback),
IntPtr.Zero);
Bitmap tempBitmap = new Bitmap(w, h);
System.Drawing.Image tempImg = System.Drawing.Image.FromHbitmap(tempBitmap.GetHbitmap());
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(tempImg);
g.Clear(Color.White);
int x, y;
x = (tempImg.Width - imag2.Width) / 2;
y = (tempImg.Height - imag2.Height) / 2;
g.DrawImage(imag2, x, y, imag2.Width, imag2.Height);
try
{
if (img != null)
img.Dispose();
if (imag2 != null)
imag2.Dispose();
if (tempBitmap != null)
tempBitmap.Dispose();
string fileExtension = System.IO.Path.GetExtension(machpath).ToLower();
//按原图片类型保存缩略图片,不按原格式图片会出现模糊,锯齿等问题.
switch (fileExtension)
{
case ".gif": tempImg.Save(SmallPath100, ImageFormat.Gif); break;
case ".jpg": tempImg.Save(SmallPath100, ImageFormat.Jpeg); break;
case ".bmp": tempImg.Save(SmallPath100, ImageFormat.Bmp); break;
case ".png": tempImg.Save(SmallPath100, ImageFormat.Png); break;
}
}
catch
{
throw new Exception("图片上传失败");
}
finally
{
//释放内存
if (tempImg != null)
tempImg.Dispose();
if (g != null)
g.Dispose();
}
}
追问
会不会出现上传图片内存不足的现象。ImageFormat这个是什么,提示说不存在这个名称
追答
ImageFormat右键,重构还是什么的,再using出来。或直接写入命名空间。using System.Drawing;
using System.Drawing2D;
没有“会不会出现上传图片内存不足的现象”。建议显示出来时,直接用客户端的标记好了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询