zoukankan      html  css  js  c++  java
  • 动态生成缩略图

    (修改自 http://community.csdn.net//Expert/TopicView2.asp?id=4370310 中所帖的代码,版权没有)

    Util.cs 的部分代码:

     1/// <summary>
     2/// 创建缩略图
     3/// </summary>
     4/// <param name="src">来源页面
     5/// 可以是相对地址或者绝对地址
     6/// </param>
     7/// <param name="width">缩略图宽度</param>
     8/// <param name="height">缩略图高度</param>
     9/// <returns>字节数组</returns>

    10public static byte[] MakeThumbnail(string src, double width, double height)
    11{
    12    Image image;
    13
    14    // 相对路径从本机直接读取
    15    if (src.ToLower().IndexOf("http://"== -1)
    16    {
    17        src = HttpContext.Current.Server.MapPath(src);
    18        image = Image.FromFile(src, true);
    19    }

    20    else // 绝对路径从 Http 读取
    21    {
    22        HttpWebRequest req = (HttpWebRequest) WebRequest.Create(src);
    23        req.Method = "GET";
    24        HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
    25        Stream receiveStream = resp.GetResponseStream();
    26        image = Image.FromStream(receiveStream);
    27        resp.Close();
    28        receiveStream.Close();
    29    }

    30    double newWidth, newHeight;
    31    if (image.Width > image.Height)
    32    {
    33        newWidth = width;
    34        newHeight = image.Height*(newWidth/image.Width);
    35    }

    36    else
    37    {
    38        newHeight = height;
    39        newWidth = (newHeight/image.Height)*image.Width;
    40    }

    41    if (newWidth > width)
    42    {
    43        newWidth = width;
    44    }

    45    if (newHeight > height)
    46    {
    47        newHeight = height;
    48    }

    49    //取得图片大小
    50    Size size = new Size((int) newWidth, (int) newHeight);
    51    //新建一个bmp图片
    52    Image bitmap = new Bitmap(size.Width, size.Height);
    53    //新建一个画板
    54    Graphics g = Graphics.FromImage(bitmap);
    55    //设置高质量插值法
    56    g.InterpolationMode = InterpolationMode.High;
    57    //设置高质量,低速度呈现平滑程度
    58    g.SmoothingMode = SmoothingMode.HighQuality;
    59    //清空一下画布
    60    g.Clear(Color.White);
    61    //在指定位置画图
    62    g.DrawImage(image, new Rectangle(00, bitmap.Width, bitmap.Height),
    63                new Rectangle(00, image.Width, image.Height),
    64                GraphicsUnit.Pixel);
    65    ///文字水印
    66    //System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
    67    //System.Drawing.Font f=new Font("宋体",10);
    68    //System.Drawing.Brush b=new SolidBrush(Color.Black);
    69    //G.DrawString("myohmine",f,b,10,10);
    70    //G.Dispose();
    71    ///图片水印
    72    //System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
    73    //Graphics a = Graphics.FromImage(bitmap);
    74    //a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
    75    //copyImage.Dispose();
    76    //a.Dispose();
    77    //copyImage.Dispose();
    78    //保存高清晰度的缩略图
    79    MemoryStream stream = new MemoryStream();
    80    bitmap.Save(stream, ImageFormat.Jpeg);
    81    byte[] buffer = stream.GetBuffer();
    82    g.Dispose();
    83    image.Dispose();
    84    bitmap.Dispose();
    85    return buffer;
    86}

    专门输出缩略图的页面 Thumbnail.aspx.cs 代码:

     1namespace JCDWeb
     2{
     3    using System;
     4    using System.Web.UI;
     5
     6    public class Thumbnail : Page
     7    {
     8        private void Page_Load(object sender, EventArgs e)
     9        {
    10            string src = GetQueryStringSrc();
    11            double width = GetQueryStringWidth();
    12            double height = GetQueryStringHeight();
    13
    14            Response.ContentType = "image/jpeg";
    15            Response.Clear();
    16
    17            if (src.Length > 0 && width > 0 && height > 0)
    18            {
    19                try 
    20                {
    21                    byte[] buffer = Util.MakeThumbnail(src, width, height);
    22                    Response.BinaryWrite(buffer);
    23                    Response.Flush();
    24                }

    25                catch
    26                {
    27
    28                }

    29            }

    30        }

    31
    32        handle query string
    72
    73        Web Forms Designer generated code
    87    }

    88}

    调用方法:

    相对地址:
    Thumbnail.aspx?width=200&height=300&src=upload/test.jpg

    绝对地址:
    Thumbnail.aspx?width=200&height=300&src=http://www.test.com/upload/test.jpg
  • 相关阅读:
    剑指Offer(Java版)第四十六题:输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序。
    剑指Offer(Java版)第四十五题:一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
    剑指Offer(Java版)第四十四题:输入一棵二叉树,判断该二叉树是否是平衡二叉树。
    剑指Offer(Java版)第四十三题:输入一棵二叉树,求该树的深度。 从根结点到叶结点依次经过的结点 (含根、叶结点)形成树的一条路径, 最长路径的长度为树的深度。
    剑指Offer(Java版)第四十二题:统计一个数字在排序数组中出现的次数。
    剑指Offer(Java版)第四十一题:输入两个链表,找出它们的第一个公共结点。
    剑指Offer(Java版)第四十题:在数组中的两个数字,如果前面一个数字大于后面的数字, 则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。 并将P对1000000007取模的结果输出。 即输出P%1000000007
    剑指Offer(Java版)第三十九题:在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符, 并返回它的位置, 如果没有则返回 -1(需要区分大小写).
    剑指Offer(Java版)第三十八题:把只包含质因子2、3和5的数称作丑数(Ugly Number)。 例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。 求按从小到大的顺序的第N个丑数。
    【微信支付】退款
  • 原文地址:https://www.cnblogs.com/RChen/p/315433.html
Copyright © 2011-2022 走看看