C#實現圖片放大功能的按照像素放大圖像方法
更新時間:2014年07月30日 17:04:50 投稿:shichen2014
這篇文章主要介紹了C#實現圖片放大功能的按照像素放大圖像方法,功能非常實用,需要的朋友可以參考下
本文實例講述了基于Visual C#實現的圖片放大功能代碼??梢灾苯臃糯笙袼兀愃苝hotoshop的圖片放大功能,可用于像素的定位及修改,由于使用了指針需要勾選允許不安全代碼選項,讀者可將其用于自己的項目中!
關于幾個參數說明:
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;
}

