本文实例讲述了C#图像处理之图像平移的方法。分享给大家供大家参考。具体如下:
//定义图像平移函数private static Bitmap offsetp(Bitmap a,int s,int v){ System.Drawing.Imaging.BitmapData srcData = a.LockBits(new Rectangle (0,0,a.Width ,a.Height) ,System .Drawing .Imaging .ImageLockMode .ReadWrite ,a.PixelFormat ); IntPtr ptr = srcData.Scan0; int bytes = srcData.Stride * a.Height; byte[]grayVlaues=new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr ,grayVlaues ,0,bytes); byte[] tempArray=new byte[bytes]; for (int i = 0; i < bytes; i++) { tempArray[i] = 255; } for (int i = 0; i < a.Width * 3; i += 3) { if ((i + s*3) < a.Width*3 && (i + s*3) > 0) { for (int j = 0; j < a.Height; j++) { if ((j + v) < a.Height && (j + v) > 0) { tempArray[(i + s * 3) + (j + v) * srcData.Stride] = grayVlaues[i + j * srcData.Stride]; tempArray[i + s * 3 + 1 + (j + v) * srcData.Stride] = grayVlaues[i + 1 + j * srcData.Stride]; tempArray[i + s * 3 + 2 + (j + v) * srcData.Stride] = grayVlaues[i + 2 + j * srcData.Stride]; } } } } grayVlaues = (byte[])tempArray.Clone(); System.Runtime.InteropServices.Marshal.Copy(grayVlaues ,0,ptr, bytes); a.UnlockBits(srcData ); return a;}
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答