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

C#清晰的图片缩略方案

希望这篇文章能给对于需要经常生成缩略图的朋友提供帮助!

购吧网目前拥有4000余种商品,在售商品超过2000万,其中图片量截至目前已有8G。
目前我们的方案是用单独的文件服务器存放商品的图片,通过file.365goba.com访问。
文件服务器上架一个用于上传图片的WCF服务对上传的图片进行缩略并保存。

购吧网前期的缩略算法用的是网略上广泛流传的三线性插值算法(效果并不是很好),代码如下:

 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;
   &n

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,