c# 怎么将上传的图像变小
图像很大只显示一个小图标怎么让他显示出来这是后台代码:publicpartialclass_Default10:Class1{protectedvoidPage_Load...
图像很大 只显示一个小图标 怎么让他显示出来
这是后台代码:
public partial class _Default10 : Class1
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string fullname = FileUpload1.FileName.ToString();
string fn = DateTime.Now.ToString("yyyyMMddHHmmss") + fullname;
string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);
string size = FileUpload1.PostedFile.ContentLength.ToString();
if (typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png")
{
FileUpload1.SaveAs(Server.MapPath("up") + "\\" + fn); //将文件保存在跟目录的UP文件夹下
Label1.Text = (fn);
Image1.ImageUrl = (Server.MapPath("up") + "\\" + fn);
}
else
{
Label1.Text = "文件类型不支持";
}
}
} 展开
这是后台代码:
public partial class _Default10 : Class1
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string fullname = FileUpload1.FileName.ToString();
string fn = DateTime.Now.ToString("yyyyMMddHHmmss") + fullname;
string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);
string size = FileUpload1.PostedFile.ContentLength.ToString();
if (typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png")
{
FileUpload1.SaveAs(Server.MapPath("up") + "\\" + fn); //将文件保存在跟目录的UP文件夹下
Label1.Text = (fn);
Image1.ImageUrl = (Server.MapPath("up") + "\\" + fn);
}
else
{
Label1.Text = "文件类型不支持";
}
}
} 展开
展开全部
给你个参考代码吧。另外。你真需要把图片缩放才需要用。这样的好处是搜索引擎收录时快照不会变形。图片真实变小也助于加载。求方便的话,直接<img src="....." width="10" height="10" />就行。下边是代码。不会用找我。
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();
}
}
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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询