首页 > 编程技术 > csharp

C#实现图片放大功能的按照像素放大图像方法

发布时间:2020-6-25 11:36

本文实例讲述了基于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;
}

标签:[!--infotagslink--]

您可能感兴趣的文章: