ASP.NET2.0图片格式转换

添加人:admin三级(1325分)   添加时间:2007-08-09    阅读次数:1023  收藏此教程

说明:本文实现了
图片格式随意转换(下拉框选择);
点击FileUpload立即显示图片(Js实现)的技巧;

 

第一步:打开页面

第二步:选择一副Jpg格式的图片

第三步:转换为GIF格式,明显看出图片画质降低。
 
 后台代码:
 
 
 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Collections;
 5using System.Web;
 6using System.Web.Security;
 7using System.Web.UI;
 8using System.Web.UI.WebControls;
 9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.IO;
12using System.Drawing;
13public partial class _Default : System.Web.UI.Page
14{
15    protected void Page_Load(object sender, EventArgs e)
16    {
17    }

18    protected void Button1_Click(object sender, EventArgs e)
19    {
20        string filepath = FileUpload1.PostedFile.FileName;
21        string filename = filepath.Substring(filepath.LastIndexOf("\") + 1);
22        string serverpath = Server.MapPath("images/") + System.DateTime.Now.ToString("yyy-MM-dd-hh-mm-ss") + Session.SessionID + filename;
23
24        if (DropDownList1.SelectedValue == "GIF")
25        {
26            ConvertImage(FileUpload1.PostedFile.FileName, System.Drawing.Imaging.ImageFormat.Gif, serverpath+".gif");
27        }

28        else if(DropDownList1.SelectedValue == "Jpeg")
29        {
30            ConvertImage(FileUpload1.PostedFile.FileName, System.Drawing.Imaging.ImageFormat.Jpeg, serverpath + ".jpg");
31        }

32        else if(DropDownList1.SelectedValue == "Bmp")
33        {
34            ConvertImage(FileUpload1.PostedFile.FileName, System.Drawing.Imaging.ImageFormat.Bmp, serverpath + ".bmp");
35        }

36        else
37        {
38            //清清月儿留给大家
39        }

40    }

41
42    public void ConvertImage(string Filename, System.Drawing.Imaging.ImageFormat DesiredFormat, string NewFilename)
43    {
44        try
45        {
46            System.Drawing.Image imgFile = System.Drawing.Image.FromFile(Filename);
47            imgFile.Save(NewFilename, DesiredFormat);
48            Image1.ImageUrl = NewFilename;
49            Label1.Text = "转换成功,生成"+NewFilename+",如下所示。";
50            TextBox1.Text = "1";//开始为0,转换后为1
51        }

52        catch (Exception ex)
53        {
54            Response.Write(ex);
55        }

56    }

57}

58
 
 
前台代码:
 
 1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2
 3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4
 5<html xmlns="http://www.w3.org/1999/xhtml" >
 6<head runat="server">
 7    <title>图片格式转换</title>
 8    <script language="javascript">
 9    function show_img()//实现选择图片后立即显示给客户
10    {
11        if(document.all.TextBox1.value=="0"){//开始为0,转换后为1
12            document.all.Image1.src=document.all.FileUpload1.value;
13        }

14        else if(document.all.TextBox1.value=="1")
15        {
16        }

17    }

18    
</script>
19</head>
20<body>
21    <form id="form1" runat="server">
22    <div>
23        &nbsp;
24        <table>
25            <tr>
26                <td style="width: 124px">
27                    <asp:FileUpload ID="FileUpload1" runat="server" onmouseover="show_img()" Width="349px"/>&nbsp;
28                </td>
29                <td style="width: 100px">
30                    格式<asp:DropDownList ID="DropDownList1" runat="server">
31                        <asp:ListItem>GIF</asp:ListItem>
32                        <asp:ListItem>Jpeg</asp:ListItem>
33                        <asp:ListItem>Bmp</asp:ListItem>
34                        <asp:ListItem>Png</asp:ListItem>
35                        <asp:ListItem>Ico</asp:ListItem>
36                    </asp:DropDownList>
37                </td>
38                <td style="width: 100px">
39                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="转换" /></td>
40                <td style="width: 100px">
41                    <asp:TextBox ID="TextBox1" runat="server">0</asp:TextBox>
42                    </td>
43            </tr>
44            <tr>
45                <td colspan="4">
46                    &nbsp;<asp:Label ID="Label1" runat="server"></asp:Label></td>
47            </tr>
48            <tr>
49                <td style="height: 26px;" colspan="4">
50                    <asp:Image ID="Image1" runat="server" /></td>
51            </tr>
52        </table>
53    
54    </div>
55    </form>
56</body>
57</html>
58
59
1页 第1上一页1下一页
相关的教程: 转换 图片 板式
收藏此教程

当前平均分: 3.0(1 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: