.aspx
<asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
<asp:Label ID="lb_info" runat="server" Text="Label"></asp:Label>
<asp:Image ID="Image1" runat="server" />
.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.PostedFile.FileName == "")
{
this.lb_info.Text = "请选择文件!";
}
else
{
byte[] fileBytePicture = new byte[FileUpload1.PostedFile.ContentLength];//用图片的长度来初始化一个字节数组存储临时的图片文件
System.IO.Stream fileStream = FileUpload1.PostedFile.InputStream;//建立文件流对象
fileStream.Read(fileBytePicture, 0, FileUpload1.PostedFile.ContentLength);
string photo = ByteToString(fileBytePicture);
//存储上传图片
string filepath = FileUpload1.PostedFile.FileName;
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
string serverpath = Server.MapPath("images/") + Guid.NewGuid().ToString() + ".jpg";
FileUpload1.PostedFile.SaveAs(serverpath);
//存储压缩图片 并返回压缩图片路径
string newImagePath = ImageThumbnail.ReducedImage(73, 73, serverpath); //缩放成73*73
//string newImagePath = ImageThumbnail.ReducedImage(0.5, serverpath);
this.lb_info.Text = "上传成功!";
//读取压缩图片 转换成string类型
using (FileStream fs = new FileStream(newImagePath, FileMode.Open, FileAccess.Read)) ////将图片以文件流的形式进行保存
{
BinaryReader br = new BinaryReader(fs);
byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //将流读入到字节数组中
string photo1 = ByteToString(imgBytesIn);
}
}
}
catch (Exception error)
{
this.lb_info.Text = "上传发生错误!原因:" + error.ToString();
}
}
/// <summary>
/// 将byte数组转为string类型
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public string ByteToString(byte[] bytes)
{
StringBuilder strBuilder = new StringBuilder();
foreach (byte bt in bytes)
{
strBuilder.AppendFormat("{0:X2}", bt);
}
return strBuilder.ToString();
}
/// <summary>
/// 图片压缩
/// </summary>
public class ImageThumbnail
{
// 方法1,按大小
/// <summary>
///按大小压缩
/// </summary>
/// <param name="Width">压缩宽</param>
/// <param name="Height">压缩高</param>
/// <param name="targetFilePath">要压缩的图片路径</param>
/// <returns>返回压缩后的图片路径</returns>
public static string ReducedImage(int Width, int Height, string targetFilePath)
{
using (Image ResourceImage = Image.FromFile(targetFilePath))
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(delegate { return false; });
ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero); //按大小压缩
string newImagePath = targetFilePath.Substring(0, targetFilePath.LastIndexOf("\\") + 1) + Guid.NewGuid().ToString() + ".jpg"; //压缩图片的存储路径
ReducedImage.Save(newImagePath, ImageFormat.Jpeg); //保存压缩图片
ReducedImage.Dispose();
return newImagePath; //返回压缩图片的存储路径
}
}
// 方法2,按百分比 缩小60% Percent为0.6 targetFilePath为目标路径
/// <summary>
/// 按百分比 压缩
/// </summary>
/// <param name="Percent">缩小比例 缩小60% Percent为0.6 </param>
/// <param name="targetFilePath">要压缩的图片路径</param>
/// <returns>返回压缩后的图片路径</returns>
public static string ReducedImage(double Percent, string targetFilePath)
{
using (Image ResourceImage = Image.FromFile(targetFilePath))
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(delegate { return false; });
int imageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
int imageHeight = Convert.ToInt32(ResourceImage.Height * Percent);//等比例缩放
ReducedImage = ResourceImage.GetThumbnailImage(imageWidth, imageHeight, callb, IntPtr.Zero);
string newImagePath = targetFilePath.Substring(0, targetFilePath.LastIndexOf("\\") + 1) + Guid.NewGuid().ToString() + ".jpg"; //压缩图片的存储路径
ReducedImage.Save(newImagePath, ImageFormat.Jpeg);
ReducedImage.Dispose();
return newImagePath;
}
}
}