C#完成圖片縮小功效的依照像素縮小圖象辦法。本站提示廣大學習愛好者:(C#完成圖片縮小功效的依照像素縮小圖象辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成圖片縮小功效的依照像素縮小圖象辦法正文
本文實例講述了基於Visual C#完成的圖片縮小功效代碼。可以直接縮小像素,相似photoshop的圖片縮小功效,可用於像素的定位及修正,因為應用了指針須要勾選許可不平安代碼選項,讀者可將其用於本身的項目中!
關於幾個參數解釋:
srcbitmap源圖片
multiple圖象縮小倍數
縮小處置後的圖片
留意:須要在頭部援用:using System.Drawing;using System.Drawing.Imaging;
至於定名空間讀者可以本身界說。
重要功效代碼以下:
using System.Drawing;using System.Drawing.Imaging;
public Bitmap Magnifier(Bitmap srcbitmap, int multiple)
{
if (multiple <= 0) { multiple = 0; return srcbitmap; }
Bitmap bitmap = new Bitmap(srcbitmap.Size.Width * multiple, srcbitmap.Size.Height * multiple);
BitmapData srcbitmapdata = srcbitmap.LockBits(new Rectangle(new Point(0, 0), srcbitmap.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData bitmapdata = bitmap.LockBits(new Rectangle(new Point(0, 0), bitmap.Size), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
byte* srcbyte = (byte*)(srcbitmapdata.Scan0.ToPointer());
byte* sourcebyte = (byte*)(bitmapdata.Scan0.ToPointer());
for (int y = 0; y < bitmapdata.Height; y++)
{
for (int x = 0; x < bitmapdata.Width; x++)
{
long index = (x / multiple) * 4 + (y / multiple) * srcbitmapdata.Stride;
sourcebyte[0] = srcbyte[index];
sourcebyte[1] = srcbyte[index + 1];
sourcebyte[2] = srcbyte[index + 2];
sourcebyte[3] = srcbyte[index + 3];
sourcebyte += 4;
}
}
}
srcbitmap.UnlockBits(srcbitmapdata);
bitmap.UnlockBits(bitmapdata);
return bitmap;
}