参考部分网上搜到的方法,自己加以修改。
1、修改Thumbnail.cs
using System;
public class Thumbnail
{
private string id;
private byte[] originalData;
private byte[] data;
public Thumbnail(string id, byte[] originalData, byte[] data)
{
this.ID = id;
this.OriginalData = originalData;
this.Data = data;
}
public string ID
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
public byte[] OriginalData
{
get
{
return this.originalData;
}
set
{
this.originalData = value;
}
}
public byte[] Data
{
get
{
return this.data;
}
set
{
this.data = value;
}
}
}
2、修改upload.aspx.cs
using System;
using System.Web;
using System.IO;
using System.Collections.Generic;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Drawing.Image thumbnail_image = null;
System.Drawing.Image original_image = null;
System.Drawing.Bitmap final_image = null;
System.Drawing.Graphics graphic = null;
MemoryStream ms = null;
MemoryStream maxms = null;
try
{
//获得数据
HttpPostedFile jpeg_image_upload = Request.Files["Filedata"];
//获得上传的图片
original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream);
int width = original_image.Width;//原始图片宽度
int height = original_image.Height;//原始图片高度
int target_width = 100;//目标图片宽度
int target_height = 100;//目标图片高度
int new_width, new_height;//新宽度和新高度
float target_ratio = (float)target_width / (float)target_height;//目标图片宽高比
float image_ratio = (float)width / (float)height;//原始图片宽高比
//计算新的长度和宽度
if (target_ratio > image_ratio)
{
new_height = target_height;
new_width = (int)Math.Floor(image_ratio * (float)target_height);
}
else
{
new_height = (int)Math.Floor((float)target_width / image_ratio);
new_width = target_width;
}
new_width = new_width > target_width ? target_width : new_width;
new_height = new_height > target_height ? target_height : new_height;
//创建缩略图
//thumbnail_image = original_image.GetThumbnailImage(new_width, new_height, null, System.IntPtr.Zero);
final_image = new System.Drawing.Bitmap(target_width, target_height);//目标图片的像素图
graphic = System.Drawing.Graphics.FromImage(final_image);//目标图片的画板
graphic.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.Black), new System.Drawing.Rectangle(0, 0, target_width, target_height));//用黑色填充目标图片大小的矩形框
int paste_x = (target_width - new_width) / 2;//从原点位移x坐标
int paste_y = (target_height - new_height) / 2;//从原点位移y坐标
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;//插补模式
//graphic.DrawImage(thumbnail_image, paste_x, paste_y, new_width, new_height);
graphic.DrawImage(original_image, paste_x, paste_y, new_width, new_height);//画图
ms = new MemoryStream();//创建一个新的内存流
final_image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//目标图片保存
maxms = new MemoryStream();//创建一个新的内存流
original_image.Save(maxms, System.Drawing.Imaging.ImageFormat.Jpeg);//原始图片保存
string thumbnail_id = DateTime.Now.ToString("yyyyMMddHHmmssfff");//根据时间生成图片名
Thumbnail thumb = new Thumbnail(thumbnail_id, maxms.ToArray(), ms.ToArray());
List<Thumbnail> thumbnails = Session["file_info"] as List<Thumbnail>;
if (thumbnails == null)
{
thumbnails = new List<Thumbnail>();
Session["file_info"] = thumbnails;
}
thumbnails.Add(thumb);
Response.StatusCode = 200;
Response.Write(thumbnail_id);
}
catch
{
Response.StatusCode = 500;
Response.Write("An error occured");
Response.End();
}
finally
{
if (final_image != null) final_image.Dispose();
if (graphic != null) graphic.Dispose();
if (original_image != null) original_image.Dispose();
if (thumbnail_image != null) thumbnail_image.Dispose();
if (ms != null) ms.Close();
if (maxms != null) maxms.Close();
Response.End();
}
}
}
3、上传事件
protected void btnSave_Click(object sender, EventArgs e)
{
if (Session["file_info"] != null)
{
List<Thumbnail> thumbnails = Session["file_info"] as List<Thumbnail>;
string UploadPath1 = Server.MapPath("HouseUploads/");
string UploadPath2 = Server.MapPath("HouseUploads/Small/");
string name1;
string name2;
StringBuilder buffer = new StringBuilder();
foreach (Thumbnail img in thumbnails)
{
name1 = img.ID + ".jpg";
name2 = "s_" + img.ID + ".jpg";
FileStream fs1 = new FileStream(UploadPath1 + img.ID + ".jpg", FileMode.Create);
BinaryWriter bw1 = new BinaryWriter(fs1);
bw1.Write(img.OriginalData);
bw1.Close();
fs1.Close();
FileStream fs2 = new FileStream(UploadPath2 + "s_" + img.ID + ".jpg", FileMode.Create);
BinaryWriter bw2 = new BinaryWriter(fs2);
bw2.Write(img.Data);
bw2.Close();
fs2.Close();
buffer.Append(name1 + "|");
}
Session.Clear();
//以下为向数据库添加图片名称的代码,可自行修改。
if (HouseBLL.UpdatePhoto(housePhoto, buffer.ToString(), true, houseId))
{
labMessage.Text = "图片上传成功!";
housePhoto = HouseBLL.GetHousePhoto(houseId);
BindHousePhoto();
}
else
{
labMessage.Text = "图片上传失败!";
housePhoto = HouseBLL.GetHousePhoto(houseId);
BindHousePhoto();
}
}
}
经测试,目前还未发现什么问题。
swfupload实现图片和缩略图上传。并将图片名保存到数据库