ASP. NET2.0 image format conversion
Author: qingyueer
Home page:Http://blog.csdn.net/21aspnet/Time: 2007.4.20
Note: This article implements
Convert the image format as needed (select from the drop-down list );
Click FileUpload to immediately display the image (Js implementation) skills;
Step 1: Open the page
Step 2: select a Jpg image
Step 3: convert to the GIF format to see that the image quality is reduced.
Background code:
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. Drawing;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void button#click (object sender, EventArgs e)
{
String filepath = FileUpload1.PostedFile. FileName;
String filename = filepath. Substring (filepath. LastIndexOf ("//") + 1 );
String serverpath = Server. mapPath ("images/") + System. dateTime. now. toString ("yyy-MM-dd-hh-mm-ss") + Session. sessionID + filename;
If (DropDownList1.SelectedValue = "GIF ")
{
ConvertImage (FileUpload1.PostedFile. FileName, System. Drawing. Imaging. ImageFormat. Gif, serverpath + ". gif ");
}
Else if (DropDownList1.SelectedValue = "Jpeg ")
{
ConvertImage (FileUpload1.PostedFile. FileName, System. Drawing. Imaging. ImageFormat. Jpeg, serverpath + ". jpg ");
}
Else if (DropDownList1.SelectedValue = "Bmp ")
{
ConvertImage (FileUpload1.PostedFile. FileName, System. Drawing. Imaging. ImageFormat. Bmp, serverpath + ". bmp ");
}
Else
{
// Clear moon left for everyone http://blog.csdn.net/21aspnet
}
}
Public void ConvertImage (string Filename, System. Drawing. Imaging. ImageFormat DesiredFormat, string NewFilename)
{
Try
{
System. Drawing. Image imgFile = System. Drawing. Image. FromFile (Filename );
ImgFile. Save (NewFilename, DesiredFormat );
Image1.ImageUrl = NewFilename;
Label1.Text = "converted successfully, generate" + NewFilename + ", as shown below. ";
TextBox1.Text = "1 ";//Starts from 0, and is converted to 1
}
Catch (exception ex)
{
Response. Write (Ex );
}
}
}
Front-end code:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> image format conversion </title>
<Script language = "JavaScript">
Function show_img ()//The image is displayed to the customer immediately after the image is selected.
{
If (document. All. textbox1.value = "0 "){//Starts from 0, and is converted to 1
Document. All. image1.src = Document. All. fileupload1.value;
}
Else if (document. All. textbox1.value = "1 ")
{
}
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
& Nbsp;
<Table>
<Tr>
<Td style = "width: 124px">
<Asp: FileUpload ID = "FileUpload1" runat = "server"Onmouseover = "show_img ()"Width = "349px"/> & nbsp;
</Td>
<Td style = "width: 100px">
Format <asp: DropDownList ID = "DropDownList1" runat = "server">
<Asp: listitem> GIF </ASP: listitem>
<Asp: listitem> JPEG </ASP: listitem>
<Asp: listitem> BMP </ASP: listitem>
<Asp: listitem> PNG </ASP: listitem>
<Asp: listitem> ICO </ASP: listitem>
</ASP: dropdownlist>
</TD>
<TD style = "width: 100px">
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = ""/> </TD>
<TD style = "width: 100px">
<Asp: textbox id = "textbox1" runat = "server"> 0 </ASP: textbox>
</Td>
</Tr>
<Tr>
<Td colspan = "4">
& Nbsp; <asp: Label ID = "Label1" runat = "server"> </asp: Label> </td>
</Tr>
<Tr>
<Td style = "height: 26px;" colspan = "4">
<Asp: Image ID = "Image1" runat = "server"/> </td>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>