Click here to Skip to main content
15,904,926 members
Articles / Multimedia / Image Processing
Tip/Trick

Faster way i Know for thumbnail (low quality)

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
9 Aug 2011CPOL 34.9K   6   6
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)
C#
//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)


Written By
Software Developer (Senior)
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: I would totally agree with Walt and Adrian Pin
DrABELL2-Aug-11 3:12
DrABELL2-Aug-11 3:12 
GeneralHey, the average time of ThumbnailLow shall be "around 6 to ... Pin
wmjordan9-Aug-11 20:47
professionalwmjordan9-Aug-11 20:47 
GeneralRe: of course :D,sorry Pin
Shargon_859-Aug-11 20:59
Shargon_859-Aug-11 20:59 
of course Big Grin | :-D ,sorry
GeneralYou might want to mention why it's faster and what you compa... Pin
Dr.Walt Fair, PE30-Jul-11 16:52
professionalDr.Walt Fair, PE30-Jul-11 16:52 
GeneralRe: My thoughts exactly Pin
Adrian Cole1-Aug-11 20:56
Adrian Cole1-Aug-11 20:56 
GeneralRe: I would totally agree with Walt and Adrian Pin
DrABELL2-Aug-11 3:13
DrABELL2-Aug-11 3: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.