SWFUpload实现原图及缩略图上传保存清空

 1.修改类 Thumbnail

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
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
        
{
            
// Get the data
            HttpPostedFile jpeg_image_upload = Request.Files["Filedata"];

            
// Retrieve the uploaded image
            original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream);
            
            
// Calculate the new width and height
            int width = original_image.Width;
            
int height = original_image.Height;
            
int target_width = 100;
            
int target_height = 75;
            
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;


            
// Create the thumbnail

            
// Old way
            
//thumbnail_image = original_image.GetThumbnailImage(new_width, new_height, null, System.IntPtr.Zero);
            
// We don't have to create a Thumbnail since the DrawImage method will resize, but the GetThumbnailImage looks better
            
// I've read about a problem with GetThumbnailImage. If a jpeg has an embedded thumbnail it will use and resize it which
            
//  can result in a tiny 40x40 thumbnail being stretch up to our target size


            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(00, target_width, target_height));
            
int paste_x = (target_width - new_width) / 2;
            
int paste_y = (target_height - new_height) / 2;
            graphic.InterpolationMode 
= System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; /* new way */
            
//graphic.DrawImage(thumbnail_image, paste_x, paste_y, new_width, new_height); 
            graphic.DrawImage(original_image, paste_x, paste_y, new_width, new_height);

            
// Store the thumbnail in the session (Note: this is bad, it will take a lot of memory, but this is just a demo)#

            

            ms 
= new MemoryStream();
            
//final_image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

            final_image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            
修改部分开始 修改部分结束

            
// Put it all in the Session (initialize the session if necessary)            
            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
        
{
            
// If any kind of error occurs return a 500 Internal Server error
            Response.StatusCode = 500;
            Response.Write(
"An error occured");
            Response.End();
        }

        
finally
        
{
            
// Clean up
            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();
            Response.End();
        }

    
    }

}

 

2.修改upload.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI;
/// <summary>
/// Summary description for Thumbnail
/// </summary>

public class Thumbnail
{
    
public Thumbnail(string id, byte[] data, byte[] minidata)
    
{
        
this.ID = id;
        
this.Data = data;
        
this.miniData = minidata;
    }


    
private string id;
    
public string ID
    
{
        
get
        
{
            
return this.id;
        }

        
set
        
{
            
this.id = value;
        }

    }


    
private byte[] max_data;
    
public byte[] Data
    
{
        
get
        
{
            
return this.max_data;
        }

        
set
        
{
            
this.max_data = value;
        }

    }


    
private byte[] thumbnail_data;
    
public byte[] miniData
    
{
        
get
        
{
            
return this.thumbnail_data;
        }

        
set
        
{
            
this.thumbnail_data = value;
        }

    }
    
}

 

3.保存按钮事件

    protected void btnSave_Click(object sender, EventArgs e)
    
{
        
if (Session["file_info"!= null)
        
{
            List
<Thumbnail> thumbnails = Session["file_info"as List<Thumbnail>;

            
string UploadPath = Server.MapPath("~/picupload/");
            
string strPicName;
            
string strPicMinName;
            
string strCID;
            strCID 
= ddlpic.SelectedItem.Value;
            TArticle CA 
= new TArticle();
            
foreach (Thumbnail img in thumbnails)
            
{
                strPicName 
= img.ID + ".jpg";
                strPicMinName 
= img.ID + "_mini.jpg";
                FileStream fs 
= new FileStream(UploadPath + img.ID + ".jpg", FileMode.Create);
                BinaryWriter bw 
= new BinaryWriter(fs);
                bw.Write(img.Data);
                bw.Close();
                fs.Close();
                FileStream fss 
= new FileStream(UploadPath + img.ID + "_mini.jpg", FileMode.Create);
                BinaryWriter bww 
= new BinaryWriter(fss);
                bww.Write(img.miniData);
                bww.Close();
                fss.Close();
                
//共用一个表文章表,原图片使用title,缩略图使用abstract
                
//栏目分类使用状态为0的
                CA.ArticleCID = strCID;
                CA.ArticleTitle 
= strPicName;
                CA.ArticleAbstract 
= strPicMinName;
                CA.PicInsert();
            }

            Session.Remove(
"file_info");
            Response.Write(
"<script>alert('图片上传成功!');</script>");
            
        }

    }

 

总结:

int target_width = 100; //缩略图宽

int target_height = 75; //缩略图高

1.修改类Thumbnail,添加缩略图成员

2.修改上传控件,创建内存流存储缩略图数据流

3.上传保存,从Thumbnail类中读取数据流,使用文件流保存图片

    并保存到数据库中。

 下载源码

 

posted @ 2009-05-21 15:03  X-Jonney  阅读(3682)  评论(5编辑  收藏  举报