Click here to Skip to main content
13,398,051 members (43,261 online)
Click here to Skip to main content
Add your own
alternative version

Stats

26.9K views
6 bookmarked
Posted 29 Jul 2011

Faster way i Know for thumbnail (low quality)

, 9 Aug 2011
Rate this:
Please Sign up or sign in to vote.
Calculate byte length for PixelFormat
private static byte BytesPerPixel(PixelFormat px)
        {
            switch (px)
            {
                case PixelFormat.Format16bppRgb555:
                case PixelFormat.Format16bppRgb565:
                case PixelFormat.Format16bppGrayScale:
                case PixelFormat.Format16bppArgb1555: return 2;
                case PixelFormat.Format24bppRgb: return 3;
                case PixelFormat.Format32bppRgb: return 4;
                case PixelFormat.Format32bppArgb: return 4;
                case PixelFormat.Format32bppPArgb: return 4;
            }
            return 0;
        }


Copy to resized Image
public static Bitmap ThumbnailLow(Bitmap img, Size sz)
        {
            if (img == null || sz.IsEmpty) return null;
            if (sz.Width > img.Width) sz = new Size(img.Width, sz.Height);
            if (sz.Height > img.Height) sz = new Size(sz.Width, img.Height);
            PixelFormat px = img.PixelFormat;
            PixelFormat pxn = PixelFormat.Format16bppRgb565;
            if (px == PixelFormat.Format32bppArgb || px == PixelFormat.Format32bppPArgb ||
                px == PixelFormat.Format32bppRgb) pxn = PixelFormat.Format24bppRgb;
            byte bytesPerPixel = BytesPerPixel(px), bytesPerPixel2 = BytesPerPixel(pxn);
            Bitmap nueva = new Bitmap(sz.Width, sz.Height, pxn);
            BitmapData bmpData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, px);
            BitmapData bmpData2 = nueva.LockBits(new Rectangle(0, 0, sz.Width, sz.Height), ImageLockMode.ReadWrite, pxn);
            //double less speed
            float inc_djn = img.Width / (float)sz.Width;
            float inc_din = img.Height / (float)sz.Height;
            float din = 0, djn = 0;
            bool _16bits = bytesPerPixel == 2 || bytesPerPixel2 == 2;
            unsafe
            {
                byte* ptr = (byte*)(bmpData.Scan0);
                byte* ptr2 = (byte*)(bmpData2.Scan0);
                int nOffset = bmpData.Stride - (bmpData.Width * bytesPerPixel);
                int nOffset2 = bmpData2.Stride - (bmpData2.Width * bytesPerPixel2);
                int h = bmpData.Height, w = bmpData.Width;
                for (int i = 0; i < h; i++)
                {
                    bool lok = i >= din;
                    djn = 0;
                    for (int j = 0; j < w; j++)
                    {
                        if (lok && j >= djn)
                        {
                            ptr2[0] = ptr[0];
                            ptr2[1] = ptr[1];
                            if (!_16bits) ptr2[2] = ptr[2];
                            ptr2 += bytesPerPixel2;
                            djn += inc_djn;
                        }
                        ptr += bytesPerPixel;
                    }
                    if (lok) { ptr2 += nOffset2; din += inc_din; }
                    ptr += nOffset;
                }
            }
            img.UnlockBits(bmpData);
            nueva.UnlockBits(bmpData2);
            return nueva;
        }



Test! (in debug mode)
//copy from screen
            Rectangle rc = Screen.PrimaryScreen.Bounds;
            Bitmap img = new Bitmap(rc.Width, rc.Height, PixelFormat.Format16bppRgb555);
            using (Graphics memoryGrahics = Graphics.FromImage(img))
                memoryGrahics.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);
            //img size 1280x1024
            Stopwatch sw = new Stopwatch(), sw2 = new Stopwatch();
            
            //non-proportional
            sw.Start();
            Image i1 = ThumbnailLow(img, new Size(100, 100)); sw.Stop();// 7 ms
            sw2.Start();
            Image i2 = img.GetThumbnailImage(100, 100, null, IntPtr.Zero); sw2.Stop();//15 ms
            //proportional
            sw.Reset(); sw.Start();
            i1 = ThumbnailLow(img, new Size(128, 102)); sw.Stop();// 6 ms
            sw2.Reset(); sw2.Start();
            i2 = img.GetThumbnailImage(128, 102, null, IntPtr.Zero); sw2.Stop();//15 ms


For img size 1280x1024

ThumbnailLow : 6 to 7 ms
GetThumbnailImage: 15 ms


Pros.... the quality is the same for videos or images in both cases
Opposites.... in text mode... becomes hard to read

if you want speed this is your method, but if you wants qualitty definitely not

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shargon_85
Software Developer (Senior)
Spain Spain
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralRe: I would totally agree with Walt and Adrian Pin
DrABELL2-Aug-11 4:12
memberDrABELL2-Aug-11 4:12 
GeneralHey, the average time of ThumbnailLow shall be "around 6 to ... Pin
wmjordan9-Aug-11 21:47
memberwmjordan9-Aug-11 21:47 
GeneralRe: of course :D,sorry Pin
Shargon_859-Aug-11 21:59
memberShargon_859-Aug-11 21:59 
GeneralYou might want to mention why it's faster and what you compa... Pin
Walt Fair, Jr.30-Jul-11 17:52
subeditorWalt Fair, Jr.30-Jul-11 17:52 
GeneralRe: My thoughts exactly Pin
Adrian Cole1-Aug-11 21:56
memberAdrian Cole1-Aug-11 21:56 
GeneralRe: I would totally agree with Walt and Adrian Pin
DrABELL2-Aug-11 4:13
memberDrABELL2-Aug-11 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180216.1 | Last Updated 10 Aug 2011
Article Copyright 2011 by Shargon_85
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid