(转)asp.net上传图片并同时生成缩略图的代码

上传图片到数据库
using System;
using System.Data;
using System.Drawing;
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;

public partial class upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void button2_click(object sender, EventArgs e)
    {
        string caption = textbox2.Text;
        bool ispublic = false;
        if (checkbox1.Checked)
            ispublic = true;
        Addalbum(caption, ispublic);
        label1.Text = "album is ok!";
    }

    public static void Addalbum(string caption, bool ispublic)
    {
        SqlConnection connection = new SqlCannection(ConfigurationManager.ConnectionStrings["personal"].ConnectionString);
        string sql;
        sql = "insert into [albums] ([caption],[ispublic]) values (@caption,@ispublic)";
        SqlCommand command = new SqlCommand(sql, connection);
        command.Parameters.Add(new SqlParameter("@caption", caption));
        command.Parameters.Add(new SqlParameter("@ispublic", ispublic));
        connection.Openn();
        command.ExecuteNonQuery();
    }
    protected void button1_click(object sender, EventArgs e)
    {
        Stream imgdatastream = fileupload1.PostedFile.InputStream;
        int imgdatalen = fileupload1.PostedFile.ContentLength;
        string caption = texbox1.Text;
        byte[] bytesoriginal = new byte[imgdatalen];
        int n = imgdatastream.Read(bytesoriginal, 0, imgdatalen);

        addphoto(1, caption, bytesoriginal);
        label1.Text = "image is uploaded ";
    }

    public static void addphoto(int albumid, string caption, byte[] bytesoriginal)
    {
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["personal"].ConnectionString);
        string sql;
        sql = "insert into [photos] ([albumid],[bytesoriginal],[caption],[bytesfull],[bytesposter],[bytesthumb])" + "values (@albumid,@bytesoriginal,@caption,@bytesfull,@bytesposter,@bytesthumb)";

        SqlCommand command = new SqlCommand(sql.connection);
        command.Parameters.Add(new SqlParameter("@albumid", albumid));
        command.Parameters.Add(new SqlParameter("@caption", caption));
        command.Parameters.Add(new SqlParameter("@bytesoriginal", bytesoriginal));
        command.Parameters.Add(new SqlParameter("@bytesfull", ReSizeImageFile(bytesoriginal, 600)));
        command.Parameters.Add(new SqlParameter("@bytesposter", ReSizeImageFile(bytesoriginal, 198)));
        command.Parameters.Add(new SqlParameter("@bytesthumb", ReSizeImageFile(bytesoriginal, 100)));
        connection.Open();
        command.ExecuteNonQuery();
    }

    private static byte[] ReSizeImageFile(byte[] imagefile, int targetSize)
    {
        System.Drawing.Image oldimage = System.Drawing.Image.FromStream(new MemoryStream(imagefile));
        Size newSize = CalculateDimensions(oldimage.Size, targetSize);
        bitmap newimage = new bitmap(newSize, width, newSize, height, PixelFormat.Format24bpprgb);
        graphics canvas = graphics.Fromimage(newimage);
        canvas.SmoothingMode = SmoothingMode.AntiAlias;
        canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
        canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
        canvas.DrawImage(oldimage, new RectangleHotSpot(new point(0, 0), newSize));
        MemoryStream m = new MemoryStream();
        newimage.Save(m, ImageFormat.Jpeg);
        return m.GetBuffer();
    }

    private static Size CalculateDimensions(Size oldSize,int targetSize)
    {
        Size newSize=new Size();
        if (oldSize.Height>oldSize.Width)
        {
            newSize.Width=(int)(oldSize.Width*((float)targetSize/(float)oldSize.Height));
            newSize.Height=targetSize;
        }
        else
        {
            newSize.Width=targetSize;
            newSize.Height=(int)(oldSize.Height*((float)targetSize/(float)oldSize.Width));
        }
        return newSize;
    }
}


'生成缩略图
image=System.Drawing.Image.FromFile(Server.MapPath("classpic/"+"rs1.jpg"))
width=image.Width
height=image.height
if width>height then
newwidth=110
newheight=image.height/image.Width*newwidth
else
newheight=110
newwidth=image.Width/image.height*newheight
end if

aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr())
aNewImage.Save(Server.MapPath("smallpic/"+"rs1.gif"))
image.Dispose()

posted @ 2010-03-23 09:39  PROS  阅读(241)  评论(0)    收藏  举报