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);
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)
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);
Stopwatch sw = new Stopwatch(), sw2 = new Stopwatch();
sw.Start();
Image i1 = ThumbnailLow(img, new Size(100, 100)); sw.Stop();
sw2.Start();
Image i2 = img.GetThumbnailImage(100, 100, null, IntPtr.Zero); sw2.Stop();
sw.Reset(); sw.Start();
i1 = ThumbnailLow(img, new Size(128, 102)); sw.Stop();
sw2.Reset(); sw2.Start();
i2 = img.GetThumbnailImage(128, 102, null, IntPtr.Zero); sw2.Stop();
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