当前位置:编程学习 > C#/ASP.NET >>

图片缩放

public static void Thumbnail (int int_Width, int int_Height, string input_ImgFile, string out_ImgFile)    
        {    
            
            System.Drawing.Image oldimage = System.Drawing.Image.FromFile(input_ImgFile);    
            float New_Width; // 新的宽度    
            float New_Height; // 新的高度    
            float Old_Width,Old_Height; //原始高宽    
            int flat = 0;//标记图片是不是等比    
               
   
            int xPoint = 0;//若果要补白边的话,原图像所在的x,y坐标。    
            int yPoint=0;    
            //判断图片    
   
            Old_Width = (float)oldimage.Width;    
            Old_Height = (float)oldimage.Height;    
   
            if ((Old_Width / Old_Height) > ((float)int_Width / (float)int_Height)) //当图片太宽的时候    
            {    
                New_Height = Old_Height * ((float)int_Width / (float)Old_Width);    
                New_Width = (float)int_Width;    
                 //此时x坐标不用修改    
                yPoint = (int)(((float)int_Height - New_Height) / 2);    
                flat = 1;    
            }    
            else if ((oldimage.Width / oldimage.Height) == ((float)int_Width / (float)int_Height))    
            {    
                New_Width = int_Width;    
                New_Height = int_Height;    
            }    
            else   
            {    
                New_Width = (int)oldimage.Width * ((float)int_Height / (float)oldimage.Height);  //太高的时候   
                New_Height = int_Height;    
                //此时y坐标不用修改    
                xPoint = (int)(((float)int_Width - New_Width) / 2);    
                flat = 1;    
            }                    
   
                       // ===缩小图片===    
            System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage((int)New_Width, (int)New_Height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);    
            Bitmap bm = new Bitmap(thumbnailImage);    
   
            if (flat != 0)    
            {    
                Bitmap bmOutput = new Bitmap(int_Width,int_Height);    
                Graphics gc = Graphics.FromImage(bmOutput);    
                SolidBrush tbBg = new SolidBrush(Color.White);    
                gc.FillRectangle(tbBg,0, 0, int_Width, int_Height); //填充为白色    
   
  
                gc.DrawImage(bm,xPoint, yPoint, (int)New_Width, (int)New_Height);    
                bmOutput.Save(out_ImgFile);    
            }    
            else   
            {    
                bm.Save(out_ImgFile);    
            }
                       oldimage.Dispose();
        }   

--------------------编程问答-------------------- 标红的那部分判断是什么意思,缩放的时候按比例缩放不是就可以了吗?为什么还要判断呢? --------------------编程问答-------------------- 你上传的图片的大小和你要缩放所指定的图片大小比例肯定会有可能不一样的,如果一样,
if ((oldimage.Width / oldimage.Height) == ((float)int_Width / (float)int_Height))这个判断直接使用你指定的宽度和长度生成缩略图进行保存。
否则就要根据指定的图片大小计算等比例的长度和宽度生成一个暂时的缩略图,然后根据指定的宽度和高度声明一个Bitmap对象,把刚才的缩略图利用Graphics画布画到Bitmap上。(就相当于你定义了一个空白图片,而这个空白图片才是你实际指定的大小图片,然后把等比例生成的图片在画到这个空白图片上,在进行保存)
举个例子:1:假如指定的高度100,宽度也是100,高度和宽度的比例值是1:1。
实际上传图片的大小高200,宽200,那么比例值也是1:1,所有直接就可以等比例生成缩略图。

2:假如指定的高度100,宽度也是100,高度和宽度的比例值是1:1。
实际上传图片的大小高200,宽100,高度和宽度的比例值是2:1,他们的比例值不一样,但要等比例生成的话
等比例的缩略图高度应该是100,宽度50才可以。
但是指定的宽度是100,少了50就要用白边填充了,所以就相当于自己定义了一个100,100的图片里画了一个100,50的图片,楼主懂了吧。
弱弱的说句,楼主数学没学好哦!
--------------------编程问答-------------------- 给你一个我的方法,直接复制粘贴看看效果就明白了
页面内容:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageThumbnail.aspx.cs" Inherits="ImageThumbnail" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <img id="RroImage" runat="server" src="~/Images/加菲猫.gif" />
    </div>
    </form>
</body>
</html>
后台代码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

public partial class ImageThumbnail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string FolderDirectory = DateTime.Now.ToString("yyyyMMdd"); //所要创建文件夹的名字
        string FolderPath = Server.MapPath("Images/" + FolderDirectory);
        if (!Directory.Exists(FolderPath)) //如果文件夹不存在则创建
        {
            Directory.CreateDirectory(FolderPath);
        }
        string filePath = Server.MapPath("Images/" + FolderDirectory + "/" + DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + ".jpg");

        Thumbnail(128, 95, Server.MapPath(RroImage.Src), filePath);
    }

    public bool ThumbnailCallback()
    {
        return false;
    }

    /// <summary>
    /// 生成缩略图,如果与指定比例不是等比例就自动补白边
    /// </summary>
    /// <param name="int_Width">指定宽度</param>
    /// <param name="int_Height">指定高度</param>
    /// <param name="input_ImgFile">原图片的路径</param>
    /// <param name="out_ImgFile">缩小后图片的路径</param>
    public void Thumbnail(int int_Width, int int_Height, string input_ImgFile, string out_ImgFile)
    {
        if (File.Exists(input_ImgFile))
        {
            System.Drawing.Image oldimage = System.Drawing.Image.FromFile(input_ImgFile);
            float New_Width;// 新的宽度
            float New_Height;// 新的高度
            float Old_Width, Old_Height;//原始高宽
            int flat = 0;//标记图片是不是等比例

            int xPoint = 0;//若果要补白边的话,原图像所在的x,y坐标。
            int yPoint = 0;
            //判断图片

            Old_Width = (float)oldimage.Width;
            Old_Height = (float)oldimage.Height;

            if ((Old_Width / Old_Height) > ((float)int_Width / (float)int_Height))//当图片宽度太宽的时候
            {
                New_Height = Old_Height * ((float)int_Width / (float)Old_Width);
                New_Width = (float)int_Width;
                //此时x坐标不用修改
                yPoint = (int)(((float)int_Height - New_Height) / 2);
                flat = 1;
            }
            else if ((oldimage.Width / oldimage.Height) == ((float)int_Width / (float)int_Height))//当图片等比例的时候
            {
                New_Width = int_Width;
                New_Height = int_Height;
            }
            else//当图片高度太高的时候
            {
                New_Width = Old_Width * ((float)int_Height / (float)oldimage.Height);
                New_Height = (float)int_Height;
                //此时y坐标不用修改
                xPoint = (int)(((float)int_Width - New_Width) / 2);
                flat = 1;
            }

            //返回缩略图图片
            System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage((int)New_Width, (int)New_Height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
            Bitmap bm = new Bitmap(thumbnailImage);

            if (flat != 0)//如果与指定比例不是等比例就自动补白边
            {
                Bitmap bmOutput = new Bitmap(int_Width, int_Height);
                Graphics gc = Graphics.FromImage(bmOutput);
                SolidBrush tbBg = new SolidBrush(Color.White);
                gc.FillRectangle(tbBg, 0, 0, int_Width, int_Height);//填充为白色

                gc.DrawImage(bm, xPoint, yPoint, (int)New_Width, (int)New_Height);
                bmOutput.Save(out_ImgFile);
            }
            else
            {
                bm.Save(out_ImgFile);
            }
        }
    }
}
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,