zoukankan      html  css  js  c++  java
  • 清晰的图片缩略方案

    网略上广泛流传的三线性插值算法(效果并不是很好),代码如下:
    using System;
    using System.Drawing;
    using System.IO;
    using System.Drawing.Imaging;
    using System.Drawing.Drawing2D;
    
    namespace Ants.Tools
    {
       
        public class Image
        {
    
            public int Width { get; set; }
    
            public int Height { get; set; }      
       
            private Image() { }
            public Image(int width, int height)
            {        
                this.Width = width;
                this.Height = height;
            }
              
            public MemoryStream getHightThumb(Stream imgData, string Mode_HW_W_H_Cut)
            {
                MemoryStream result = new MemoryStream();
                System.Drawing.Image.GetThumbnailImageAbort myCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallBack);
                try
                {
                    System.Drawing.Image originalImage = System.Drawing.Image.FromStream(imgData);
    
                    int X, Y;
                    X = Width;
                    Y = Height;
    
                    int towidth = X;
                    int toheight = Y;
    
                    int x = 0;
                    int y = 0;
                    int ow = originalImage.Width;
                    int oh = originalImage.Height;
    
                    switch (Mode_HW_W_H_Cut)
                    {
                        case "HW": //指定高宽缩放(可能变形)
      break; case "W"://指定宽,高按比例 toheight = originalImage.Height * X / originalImage.Width;
    break; case "H//指定高,宽按比例
                            towidth = originalImage.Width * Y / originalImage.Height;
                           
    break; case "Cut": if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * Y / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(System.Drawing.Color.Transparent); g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); bitmap.Save(result, System.Drawing.Imaging.ImageFormat.Jpeg); } catch { //do something } return result; }
        }
    }
    此算法可以满足一般的网站的需求,但是作为一个电子商务网站,商品的图片的清晰度直接影响到消费都对此商品的购买欲望。
    为了找到更好的方案,终于让我们找到了一个好的组件:MagickNet 这个组件是用C++写的,不过网络上已经有可用的C#调用版,文章的
    后我也会给出这个DLL文件,值得一提的是这个组件是开源的。大家可以去研究下。
    MagickNet 的功能很多,我这里就贴出一下他的缩略方法的用法(MagickNet 的资料在网上很难早)
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace KeChenDLL
    {
        public class UploadFile
        {
            private string _path;//要处理的图片(原图)地址
            public UploadFile(string path)
            {
                this._path = path;
            }
            private UploadFile()
            {
               
            }
            public void ReSize(int width, int height,string SaveToPath)
            {
                MagickNet.Image img = new MagickNet.Image(_path);
                img.Quality = 100;
    
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(_path);
                int x = bitmap.Width;
                int y = bitmap.Height;
                float rank = ((float) x)/y;
                if (x > y)
                {
                    height =Convert.ToInt32(height / rank);
                }
                else
                {
                    width =Convert.ToInt32(width * rank);
                }
    
                img.Resize(new System.Drawing.Size(width, height));
                img.Write(SaveToPath);
                img.Dispose();
            }
        }
    }
    希望这篇文章能帮到需要的人。
    MagickNet组件地址

    原创文字只代表本人某一时间内的观点或结论,本人不对涉及到的任何代码担保。转载请标明出处!

  • 相关阅读:
    解题:HNOI 2008 玩具装箱
    2016级算法第一次上机助教版解题报告
    求解斐波那契数列复杂度分析
    数据库复习之规范化理论应用(第八次上机内容)
    数据库复习之规范化理论
    题目1042:Coincidence(最长公共子序列)
    题目1020:最小长方形(简单)
    题目1016:火星A+B(字符串拆分)
    题目1014:排名(结构体排序)
    题目1021:统计字符(hash简单应用)
  • 原文地址:https://www.cnblogs.com/leleroyn/p/1605541.html
Copyright © 2011-2022 走看看