ASP NET2 0图片格式转换【月儿原创】

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

 ASP.NET2.0图片格式转换

作者:清清月儿

主页:http://blog.csdn.net/21aspnet/           时间:2007.4.20

ASP NET2 0图片格式转换【月儿原创】_第1张图片

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

 

第一步:打开页面
ASP NET2 0图片格式转换【月儿原创】_第2张图片

第二步:选择一副Jpg格式的图片
ASP NET2 0图片格式转换【月儿原创】_第3张图片

第三步:转换为GIF格式,明显看出图片画质降低。
ASP NET2 0图片格式转换【月儿原创】_第4张图片

后台代码:

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" %>



    图片格式转换
   


   


   

         
       
           
               
               
               
               
           
           
               
           
           
               
           
       

                    οnmοuseοver="show_img()" Width="349px"/> 
               

                    格式
                        GIF
                        Jpeg
                        Bmp
                        Png
                        Ico
                   

               

                   

                    0
                   

                     

                   

   
   

   



           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

ASP NET2 0图片格式转换【月儿原创】_第5张图片

你可能感兴趣的:(ASP NET2 0图片格式转换【月儿原创】)