Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / multimedia / image-processing

Faster way i Know for thumbnail (low quality)

4.67/5 (3 votes)
9 Aug 2011CPOL 35.6K  
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)