ASP NET2 0圖片格式轉換【月兒原創】
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
ASP.NET2.0圖片格式轉換
作者:清清月兒
主頁:http://blog.csdn.net/21aspnet/ 時間:2007.4.20
說明:本文實現了
圖片格式隨意轉換(下拉框選擇);
點選FileUpload立即顯示圖片(Js實現)的技巧;
第一步:開啟頁面
第二步:選擇一副Jpg格式的圖片
第三步:轉換為GIF格式,明顯看出圖片畫質降低。
後臺程式碼:
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 Button1_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
{
//清清月兒留給大家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 = "轉換成功,生成"+NewFilename+",如下所示。";
TextBox1.Text = "1";//開始為0,轉換後為1
}
catch (Exception ex)
{
Response.Write(ex);
}
}
}
前臺程式碼:
<%@ 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>圖片格式轉換</title>
<script language="javascript">
function show_img()//實現選擇圖片後立即顯示給客戶
{
if(document.all.TextBox1.value=="0"){//開始為0,轉換後為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>
<table>
<tr>
<td style="width: 124px">
<asp:FileUpload ID="FileUpload1" runat="server" onmouseover="show_img()" Width="349px"/>
</td>
<td style="width: 100px">
格式<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="Button1_Click" Text="轉換" /></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox1" runat="server">0</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="4">
<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>