本文實(shí)例講述了基于Visual C#實(shí)現(xiàn)的圖片放大功能代碼。可以直接放大像素,類似photoshop的圖片放大功能,可用于像素的定位及修改,由于使用了指針需要勾選允許不安全代碼選項(xiàng),讀者可將其用于自己的項(xiàng)目中!
關(guān)于幾個(gè)參數(shù)說明:
srcbitmap源圖片
multiple圖像放大倍數(shù)
放大處理后的圖片
注意:需要在頭部引用: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;}新聞熱點(diǎn)
疑難解答
圖片精選