zoukankan      html  css  js  c++  java
  • 毫秒级的时间处理图片

    毫秒级的时间处理上G的图片

     

    测试环境:

    测试图片(30M):

    测试计时方法:

    复制代码
    Stopwatch sw1 = new Stopwatch();
    sw1.Start();
    //TODO......
    sw1.Stop();
    string xx = sw1.ElapsedMilliseconds.ToString();
    MessageBox.Show(xx);
    复制代码


    方法一,(1张30M图片,用时799毫秒)

    public Image getThumbNailUsingGetThumbnailImage(string fileName)
    {
        Image img = Image.FromFile(fileName);
        return img.GetThumbnailImage(300, 300, null, IntPtr.Zero);
    }


    方法二,(1张30M图片,用时1329毫秒)

    复制代码
    public Image createThumbnailUsingGDI(ref Image imgPhoto, int destWidth, int destHeight)
    {
        int sourceX = 0;
        int sourceY = 0;
    
        int destX = 0;
        int destY = 0;
        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;
    
        Bitmap b = new Bitmap(destWidth, destHeight);
    
        Graphics grPhoto = Graphics.FromImage(b);
    
        grPhoto.FillRectangle(Brushes.DarkGray, new Rectangle(destX, destY, destWidth, destHeight));
        grPhoto.DrawLine(new Pen(Brushes.LightGray), new Point(0, destHeight - 1), new Point(destWidth, destHeight - 1));
        grPhoto.DrawLine(new Pen(Brushes.LightGray), new Point(destWidth - 1, 0), new Point(destWidth - 1, destHeight));
        //shade right
        grPhoto.FillRectangle(Brushes.White, new Rectangle(destWidth - 3, 0, 7, 2));
        grPhoto.FillRectangle(Brushes.White, new Rectangle(destWidth - 2, 0, 7, 4));
        grPhoto.FillRectangle(Brushes.White, new Rectangle(destWidth - 1, 0, 7, 6));
    
        //shade botton
        grPhoto.FillRectangle(Brushes.White, new Rectangle(0, destHeight - 3, 2, 7));
        grPhoto.FillRectangle(Brushes.White, new Rectangle(0, destHeight - 2, 4, 7));
        grPhoto.FillRectangle(Brushes.White, new Rectangle(0, destHeight - 1, 6, 7));
        grPhoto.DrawImage(imgPhoto, new Rectangle(destX + 2, destY + 2, destWidth - 7, destHeight - 7), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);
    
        grPhoto.Dispose();
        return b;
    
    }
    复制代码


    方法三,(1张30M图片,用时1636毫秒)

    复制代码
    public Image getThumbNailWithFrame(string fileName)
    {
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Image im = Image.FromStream(fs);
        Size szMax = new Size(300, 300);
        Size sz = getProportionalSize(szMax, im.Size);
        // superior image quality   
        Bitmap bmpResized = new Bitmap(sz.Width, sz.Height);
        using (Graphics g = Graphics.FromImage(bmpResized))
        {
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.FillRectangle(Brushes.White, 0, 0, sz.Width, sz.Height);
            int FrameWidth = 5;//decides the frame border width
            g.DrawRectangle(new Pen(Color.Silver, FrameWidth - 2), 0, 0, sz.Width - 1, sz.Height - 1);
            FrameWidth += 5;//decide the frame width
            g.DrawImage(im, new Rectangle(FrameWidth, FrameWidth, sz.Width - FrameWidth * 2, sz.Height - FrameWidth * 2), new Rectangle(Point.Empty, im.Size), GraphicsUnit.Pixel);
        }
        im.Dispose(); im = null;
        fs.Close(); fs.Dispose(); fs = null;
        return bmpResized;
    }
    
    private Size getProportionalSize(Size szMax, Size szReal)
    {
        int nWidth;
        int nHeight;
        double sMaxRatio;
        double sRealRatio;
    
        if (szMax.Width < 1 || szMax.Height < 1 || szReal.Width < 1 || szReal.Height < 1)
            return Size.Empty;
    
        sMaxRatio = (double)szMax.Width / (double)szMax.Height;
        sRealRatio = (double)szReal.Width / (double)szReal.Height;
    
        if (sMaxRatio < sRealRatio)
        {
            nWidth = Math.Min(szMax.Width, szReal.Width);
            nHeight = (int)Math.Round(nWidth / sRealRatio);
        }
        else
        {
            nHeight = Math.Min(szMax.Height, szReal.Height);
            nWidth = (int)Math.Round(nHeight * sRealRatio);
        }
    
        return new Size(nWidth, nHeight);
    }
    复制代码


    方法四,(1张30M图片,用时1664毫秒)

    复制代码
    public Image getThumbNail(string fileName)
    {
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Image im = Image.FromStream(fs);
        Size szMax = new Size(300, 300);
        Size sz = getProportionalSize(szMax, im.Size);
        // superior image quality   
        Bitmap bmpResized = new Bitmap(sz.Width, sz.Height);
        using (Graphics g = Graphics.FromImage(bmpResized))
        {
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(
                im,
                new Rectangle(Point.Empty, sz),
                new Rectangle(Point.Empty, im.Size),
                GraphicsUnit.Pixel);
        }
        im.Dispose(); im = null;
        fs.Close(); fs.Dispose(); fs = null;
        return bmpResized;
    }
    
    private Size getProportionalSize(Size szMax, Size szReal)
    {
        int nWidth;
        int nHeight;
        double sMaxRatio;
        double sRealRatio;
    
        if (szMax.Width < 1 || szMax.Height < 1 || szReal.Width < 1 || szReal.Height < 1)
            return Size.Empty;
    
        sMaxRatio = (double)szMax.Width / (double)szMax.Height;
        sRealRatio = (double)szReal.Width / (double)szReal.Height;
    
        if (sMaxRatio < sRealRatio)
        {
            nWidth = Math.Min(szMax.Width, szReal.Width);
            nHeight = (int)Math.Round(nWidth / sRealRatio);
        }
        else
        {
            nHeight = Math.Min(szMax.Height, szReal.Height);
            nWidth = (int)Math.Round(nHeight * sRealRatio);
        }
    
        return new Size(nWidth, nHeight);
    }
    复制代码


    方法五,(1张30M图片,用时735毫秒)

    复制代码
    public Image createThumbFromProperty(string file)
    {
        Image image = new Bitmap(file);
        Image Thumb = null;
        PropertyItem[] propItems = image.PropertyItems;
        foreach (PropertyItem propItem in propItems)
        {
            if (propItem.Id == 0x501B)
            {
                byte[] imageBytes = propItem.Value;
                MemoryStream stream = new MemoryStream(imageBytes.Length);
                stream.Write(imageBytes, 0, imageBytes.Length);
                Thumb = Image.FromStream(stream);
                break;
            }
        }
        return Thumb;
    }
    复制代码


    方法六,(50张30M图片,237毫秒)

    View Code


    方法七,(50张30M图片,96毫秒)

    速度最快,只要兄弟们推荐的多,我下午公布源码。^_^ 

    测试结果:

    方法 图片张数(张) 每张大小(M) 图片总大小 用时
    方法一 50 30 1500M 39950毫秒=40秒
    方法二 50 30 1500M 66450毫秒=66秒
    方法三 50 30 1500M 81800毫秒=81秒
    方法四 50 30 1500M 83200毫秒=83秒
    方法五 50 30 1500M 36750毫秒=37秒
    方法六 50 30 1500M 237毫秒
    方法七 50 30 1500M 96毫秒
     
     
  • 相关阅读:
    小米手机4 rom 下载链接
    java 两个线程交替打印到100
    获取积分最多的人(二)
    刷题通过的题目排名
    考试分数(二)
    牛客的课程订单分析(二)
    牛客的课程订单分析(三)
    docker使用小记
    Dynamic Graph Collaborative Filtering
    Learnable Embedding Sizes for Recommender Systems
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/5565845.html
Copyright © 2011-2022 走看看