<%@ Page Language="c#" Debug="true" Trace="true"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<html>
<script runat =server>
      void UploadBtn_Click(Object sender, EventArgs e) {
         String filename;
         String filename1;
         String[] filename2;
         int q;
         filename=UploadFile.PostedFile.FileName ;
         filename2=filename.Split(new Char[] {'\\'});
         q=filename2.GetUpperBound(0);
         filename1=filename2[q];
         dis.Text="上传文件名:"+filename1+"<br>";
         UploadFile.PostedFile.SaveAs(Server.MapPath(filename1));
         ImageEditor.Visible = true;
         dis.Text+="文件大小:"+UploadFile.PostedFile.ContentLength+"字节数";
         Image1.Src=filename1;      
      } 
      void UpdateBtn_Click(Object sender, EventArgs e) {
         String filename1;
         filename1=Image1.Src;
         System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(filename1));
         System.Drawing.Image newimage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppRGB);
         Graphics g = Graphics.FromImage(newimage);
         g.DrawImage(image,0,0,image.Width,image.Height);
         Font f = new Font(FontType.SelectedItem.Text, Int32.Parse(FontSize.SelectedItem.Text));
         Brush b = new SolidBrush(Color.Red);
         g.DrawString(Caption.Text, f, b, 10, 140);
         g.Dispose();
         System.Drawing.Image thumbImage = newimage.GetThumbnailImage(Int32.Parse(Width.Text),Int32.Parse(Height.Text),null,0);
         image.Dispose();
         thumbImage.Save(Server.MapPath(filename1), ImageFormat.JPEG);
         Image1.Src=filename1;       
         Caption.Text="";     
   
      }   </script>   <body>
   <asp:label id="dis" runat=server/>
     <form enctype="multipart/form-data" runat=server>          <h3>          Select File To Upload: <input id="UploadFile" type=file runat=server>          <asp:button Text="Upload Me!" OnClick="UploadBtn_Click" runat=server/>          <hr>
      
          <asp:panel id="ImageEditor" Visible=false runat=server>             <img ID="Image1" src="" runat="server"/>             <h3>
                Image Width: <asp:textbox id="Width" runat=server/>                 Image Height: <asp:textbox id="Height" runat=server/> <br>                 Text Caption: <asp:textbox id="Caption" runat=server/>                 Caption Size: <asp:dropdownlist id="FontSize" runat=server>
                                         <asp:listitem>14</asp:listitem>
                                         <asp:listitem>18</asp:listitem>
                                         <asp:listitem>26</asp:listitem>
                                         <asp:listitem>36</asp:listitem>
                                         <asp:listitem>48</asp:listitem>
                                         <asp:listitem>62</asp:listitem>
                                      </asp:dropdownlist>
                 Caption Font: <asp:dropdownlist id="FontType" runat=server>
                                         <asp:listitem>黑体</asp:listitem>
                                         <asp:listitem>仿宋</asp:listitem>
                                         <asp:listitem>隶书</asp:listitem>
                                         <asp:listitem>楷书</asp:listitem>
                                         <asp:listitem>方正姚体</asp:listitem>
                                         <asp:listitem>华文彩云</asp:listitem>
                                      </asp:dropdownlist>
                                                
                 <asp:button Text="Update Image" OnClick="UpdateBtn_Click" runat=server/>             </h3> 
          </asp:panel>      </form>   </body>
</html>

解决方案 »

  1.   

    楼上的,首先谢谢,不过好象还有问题CS1502: 与“System.Drawing.Image.GetThumbnailImage(int, int, System.Drawing.Image.GetThumbnailImageAbort, System.IntPtr)”最匹配的重载方法具有一些无效参数能不能再帮忙看看,谢谢
      

  2.   

    下面是我改后好使的代码,不知哪为有更好的解决方法<%@ Page Language="c#" Debug="true" Trace="true"%>
    <%@ Import Namespace="System.IO" %>
    <%@ Import Namespace="System.Drawing" %>
    <%@ Import Namespace="System.Drawing.Imaging" %>
    <html>
    <script runat =server>
          void UploadBtn_Click(Object sender, EventArgs e) {
             String filename;
             String filename1;
             String[] filename2;
             int q;
             filename=UploadFile.PostedFile.FileName ;
             filename2=filename.Split(new Char[] {'\\'});
             q=filename2.GetUpperBound(0);
             filename1=filename2[q];
             dis.Text="上传文件名:"+filename1+"<br>";
             UploadFile.PostedFile.SaveAs(Server.MapPath(filename1));
             ImageEditor.Visible = true;
             dis.Text+="文件大小:"+UploadFile.PostedFile.ContentLength+"字节数";
             Image1.Src=filename1;      
          } 
         bool ThumbnailCallback()  { return false; }
          void UpdateBtn_Click(Object sender, EventArgs e) {
             String filename1;
             filename1=Image1.Src;
             System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(filename1));
             System.Drawing.Image newimage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppRgb);
             Graphics g = Graphics.FromImage(newimage);
             g.DrawImage(image,0,0,image.Width,image.Height);
             Font f = new Font(FontType.SelectedItem.Text, Int32.Parse(FontSize.SelectedItem.Text));
             Brush b = new SolidBrush(Color.Red);
             g.DrawString(Caption.Text, f, b, 10, 140);
             g.Dispose();
             System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
             System.Drawing.Image thumbImage = newimage.GetThumbnailImage(Int32.Parse(Width.Text),Int32.Parse(Height.Text),myCallback,IntPtr.Zero);
             image.Dispose();
             thumbImage.Save(Server.MapPath(filename1), ImageFormat.Jpeg);
             Image1.Src=filename1;       
             Caption.Text="";     
       
          }   </script>   <body>
       <asp:label id="dis" runat=server/>
         <form enctype="multipart/form-data" runat=server ID="Form1">          <h3>          Select File To Upload: <input id="UploadFile" type=file runat=server NAME="UploadFile">          <asp:button Text="Upload Me!" OnClick="UploadBtn_Click" runat=server ID="Button1"/>          <hr>
          
              <asp:panel id="ImageEditor" Visible=false runat=server>             <img ID="Image1" src="" runat="server"/>             <h3>
                    Image Width: <asp:textbox id="Width" runat=server/>                 Image Height: <asp:textbox id="Height" runat=server/> <br>                 Text Caption: <asp:textbox id="Caption" runat=server/>                 Caption Size: <asp:dropdownlist id="FontSize" runat=server>
                                             <asp:listitem>14</asp:listitem>
                                             <asp:listitem>18</asp:listitem>
                                             <asp:listitem>26</asp:listitem>
                                             <asp:listitem>36</asp:listitem>
                                             <asp:listitem>48</asp:listitem>
                                             <asp:listitem>62</asp:listitem>
                                          </asp:dropdownlist>
                     Caption Font: <asp:dropdownlist id="FontType" runat=server>
                                             <asp:listitem>黑体</asp:listitem>
                                             <asp:listitem>仿宋</asp:listitem>
                                             <asp:listitem>隶书</asp:listitem>
                                             <asp:listitem>楷书</asp:listitem>
                                             <asp:listitem>方正姚体</asp:listitem>
                                             <asp:listitem>华文彩云</asp:listitem>
                                          </asp:dropdownlist>
                                                    
                     <asp:button Text="Update Image" OnClick="UpdateBtn_Click" runat=server ID="Button2"/>             </h3> 
              </asp:panel>      </form>   </body>
    </html>
      

  3.   

    以下是Chris Khoo写的一段代码:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace netimageupload
    {
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.HtmlControls.HtmlInputFile filUpload;
    protected System.Web.UI.WebControls.Image imgPicture;
    protected System.Web.UI.WebControls.Label lblOutput;
    protected System.Web.UI.WebControls.Button btnUpload;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
    this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void btnUpload_Click(object sender, System.EventArgs e)
    {
    // Initialize variables
    string sSavePath;
    string sThumbExtension;
    int intThumbWidth;
    int intThumbHeight; // Set constant values
    sSavePath = "images/";
    sThumbExtension = "_thumb";
    intThumbWidth = 160;
    intThumbHeight = 120; // If file field isn抰 empty
    if (filUpload.PostedFile != null)
    {
    // Check file size (mustn抰 be 0)
    HttpPostedFile myFile = filUpload.PostedFile;
    int nFileLen = myFile.ContentLength;
    if (nFileLen == 0)
    {
    lblOutput.Text = "There wasn't any file uploaded.";
    return;
    } // Check file extension (must be JPG)
    // if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
    // {
    // lblOutput.Text = "The file must have an extension of JPG";
    // return;
    // } // Read file into a data stream
    byte[] myData = new Byte[nFileLen];
    myFile.InputStream.Read(myData,0,nFileLen); // Make sure a duplicate file doesn抰 exist.  If it does, keep on appending an incremental numeric until it is unique
    string sFilename = System.IO.Path.GetFileName(myFile.FileName);
    int file_append = 0;
    while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename)))
    {
    file_append++;
    sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + file_append.ToString() + ".jpg";
    } // Save the stream to disk
    System.IO.FileStream newFile = new System.IO.FileStream(Server.MapPath(sSavePath + sFilename), System.IO.FileMode.Create);
    newFile.Write(myData,0, myData.Length);
    newFile.Close(); // Check whether the file is really a JPEG by opening it
    System.Drawing.Image.GetThumbnailImageAbort myCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
    Bitmap myBitmap;
    try
    {
    myBitmap = new Bitmap(Server.MapPath(sSavePath + sFilename)); // If jpg file is a jpeg, create a thumbnail filename that is unique.
    file_append = 0;
    string sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + sThumbExtension + ".jpg";
    while (System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile)))
    {
    file_append++;
    sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + file_append.ToString() + sThumbExtension + ".jpg";
    } // Save thumbnail and output it onto the webpage
    System.Drawing.Image myThumbnail = myBitmap.GetThumbnailImage(intThumbWidth, intThumbHeight, myCallBack, IntPtr.Zero);
    myThumbnail.Save (Server.MapPath(sSavePath + sThumbFile));
    imgPicture.ImageUrl = sSavePath + sThumbFile; // Displaying success information
    lblOutput.Text = "File uploaded successfully!"; // Destroy objects
    myThumbnail.Dispose();
    myBitmap.Dispose();
    }
    catch (ArgumentException errArgument)
    {
    // The file wasn't a valid jpg file
    lblOutput.Text = "The file wasn't a valid jpg file.";
    System.IO.File.Delete(Server.MapPath(sSavePath + sFilename));
    }
    }
    } public bool ThumbnailCallback()
    {
    return false;
    }
    }
    }