C#上传文件带缩略图、带水印(转载)

C#上传文件带缩略图、带水印(转载)

Author :Vinceyu
Mail:vinspt928@sina.com
----------------------------------------------------------------------
upload.aspx
<HTML>
    
<HEAD>
        
<title>upload</title>
        
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        
<meta content="C#" name="CODE_LANGUAGE">
        
<meta content="JavaScript" name="vs_defaultClientScript">
        
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" encType="multipart/form-data" runat="server">
            
<INPUT id="fileUpload" type="file" name="filUpload" runat="server">
            
<asp:Button id="btnUpload" runat="server" Text="Upload" Width="96px" Height="48px"></asp:Button>
            
<asp:Image id="imgPicture" runat="server" Width="160px" Height="120px"></asp:Image>
            
<asp:Label id="lblOutput" runat="server" Width="240px" Height="112px"></asp:Label>
        
</form>
    
</body>
</HTML>




upload.asp.cs
        private void btnUpload_Click(object sender, System.EventArgs e)
        
{
            
string NormalDirectory;        //正常图片存放路径
            string SmallDirectory;        //缩略图片存放路径
            string FileName;            //图片文件名
            string ExtendName;            //图片扩展名
            int ThumbWidth;                //缩略图宽度
            int ThumbHeight;            //缩略图高度


            
//设置初始值
            NormalDirectory  = "/Picture/Products/Normal/";
            SmallDirectory 
= "/Picture/Products/Small/";
            ExtendName 
= System.IO.Path.GetExtension(fileUpload.PostedFile.FileName);
            FileName 
= DateTime.Now.ToString("yyMMddhhmmssff");
            ThumbWidth 
= 130;
            ThumbHeight 
= 130;


            
//判断路径上传是否网站
            if (NormalDirectory.Substring(NormalDirectory.Length-1,1!= "/") NormalDirectory = NormalDirectory + "/";
            
if (SmallDirectory.Substring(SmallDirectory.Length-1,1!= "/") SmallDirectory = SmallDirectory + "/";

            
//判断目录是否存在
            if (!Directory.Exists(Server.MapPath(NormalDirectory))) Directory.CreateDirectory(Server.MapPath(NormalDirectory));
            
if (!Directory.Exists(Server.MapPath(SmallDirectory))) Directory.CreateDirectory(Server.MapPath(SmallDirectory));

            
//如果存在上传文件
            if (fileUpload.PostedFile != null)
            
{
                HttpPostedFile myFile 
= fileUpload.PostedFile;
                
int nFileLen = myFile.ContentLength;

                
byte[] myData = new Byte[nFileLen];
                myFile.InputStream.Read(myData,
0,nFileLen);
                
//myFile.


                
// 加水印------------------------------------------------------------>

                
float[][] matrixItems =
                                           
new float[] {10000},
                                           
new float[] {01000},
                                           
new float[] {00100},
                                           
new float[] {000, (float)20/100f, 0}
                                           
new float[] {00001}}
;
                ColorMatrix colorMatrix 
= new ColorMatrix(matrixItems);
                ImageAttributes imgAttr
= new ImageAttributes();
                imgAttr.SetColorMatrix(colorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);


                
string wImageFile = Server.MapPath("1.gif"); // 要加的水印图
                Image sImage = Image.FromStream(myFile.InputStream); // 从 Http 输入流创建 image
                
                Image wImage 
= Image.FromFile(wImageFile);
                

                
// 绘图
                Graphics g = Graphics.FromImage(sImage);
                g.DrawImage(wImage, 
new Rectangle(00, wImage.Width, wImage.Height),-12,-4, wImage.Width+12, wImage.Height+4, GraphicsUnit.Pixel,imgAttr);
   
                
// 保存,并将 image 转化为 byte[]
                MemoryStream ms= new MemoryStream();
                
byte[] myImage=null;
                sImage.Save(ms, ImageFormat.Jpeg);
                myImage 
= ms.GetBuffer();
                
//------------------------------------------------------------------>

                System.IO.FileStream newFile 
= new System.IO.FileStream(Server.MapPath(NormalDirectory + FileName + ExtendName), System.IO.FileMode.Create);
                newFile.Write(myImage,
0, myImage.Length);
                newFile.Close();


                System.Drawing.Image.GetThumbnailImageAbort myCallBack 
= new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                Bitmap myBitmap;
                
try
                
{
                    myBitmap 
= new Bitmap(myFile.InputStream);

                    System.Drawing.Image myThumbnail 
= myBitmap.GetThumbnailImage(ThumbWidth, ThumbHeight, myCallBack, IntPtr.Zero);


                    myThumbnail.Save(Server.MapPath(SmallDirectory 
+ FileName + ExtendName),System.Drawing.Imaging.ImageFormat.Jpeg);
                    imgPicture.ImageUrl 
= SmallDirectory + FileName + ExtendName;

                    lblOutput.Text 
= "File uploaded successfully!";

                    myThumbnail.Dispose();
                    myBitmap.Dispose();
                }

                
catch (ArgumentException errArgument)
                
{
                    lblOutput.Text 
= "";
                    System.IO.File.Delete(Server.MapPath(SmallDirectory 
+ FileName + ExtendName));
                }

            }


        }


        
public bool ThumbnailCallback()
        
{
            
return false;
        }
posted @ 2010-03-04 20:30  努力的少年  阅读(315)  评论(0)    收藏  举报