SEO奇迹打造最专业的石家庄网站建设公司!提供专业的石家庄网站优化和石家庄网络推广优质服务!石家庄做网站就找石家庄SEO奇迹!
石家庄网站推广_石家庄seo就来SEO奇迹
石家庄网站建设_石家庄网站优化就来SEO奇迹
做网站就来石家庄SEO奇迹
网站首页 >> 网络编程 >> 文章内容

ASP.NET实现压缩图片成缩略图

[日期:2011-04-21]   来源:网络转载  作者:SEO奇迹   阅读:4129次[字体: ]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Imaging;
using System.Drawing; 

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateThumbnailImage();
    }
    public void CreateThumbnailImage()
    {     
    string imgFile = "e:/1.jpg";
    System.Drawing.Image img = System.Drawing.Image.FromFile(imgFile);
    System.Drawing.Image.GetThumbnailImageAbort cb =new System.Drawing.Image.GetThumbnailImageAbort(CallBack);
    System.Drawing.Image thumbnailImage = img.GetThumbnailImage(120, 120, cb, System.IntPtr.Zero);
    string extendName = imgFile.Substring(imgFile.LastIndexOf("."),1);
    ImageFormat imgFormat = GetImageFormat(extendName);
    thumbnailImage.Save("e:/2.jpg",imgFormat);
    }
    public bool CallBack()
    {
        return false;
    }
    //获得图片格式
    private static ImageFormat GetImageFormat(String extendName)
    {
        extendName = extendName.ToLower();
        if (extendName == "gif")
        {
            return ImageFormat.Gif;
        }
        else if (extendName == "jpg" || extendName == "jpeg")
        {
            return ImageFormat.Jpeg;
        }
        else if (extendName == "bmp")
        {
            return ImageFormat.Bmp;
        }
        else if (extendName == "wmf")
        {
            return ImageFormat.Emf;
        }
        else if (extendName == "exif")
        {
            return ImageFormat.Exif;
        }
        else if (extendName == "icon")
        {
            return ImageFormat.Icon;
        }
        else if (extendName == "tiff")
        {
            return ImageFormat.Tiff;
        }
        else
        {
            return ImageFormat.Png;
        }
    }
}