不放首页,都木有人看的说。。。
之前公司搞图像处理,代码都拷不出来,只剩下一点点自己平时找的和写的了。
代码全是自己写的,原理嘛,,都是从c++,java里边学来的。
1 /// <summary> 2 /// 双线性插值缩放算法 3 /// </summary> 4 /// <param name="srcImg"></param> 5 /// <param name="width"></param> 6 /// <param name="height"></param> 7 /// <returns></returns> 8 public static Image Thumbnail(Bitmap srcImg, Int32 width, Int32 height) 9 { 10 var srcData = srcImg.LockBits(new Rectangle(0, 0, srcImg.Width, srcImg.Height), ImageLockMode.ReadWrite, 11 PixelFormat.Format24bppRgb); 12 13 var destImg = new Bitmap(width, height, PixelFormat.Format24bppRgb); 14 var destData = destImg.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, 15 PixelFormat.Format24bppRgb); 16 Int32 x, y; 17 Double sx, sy, u, v; 18 var pm = new Double[4]; 19 20 Double w = (Double)srcImg.Width / (Double)width; 21 Double h = (Double)srcImg.Height / (Double)height; 22 23 for (int row = 0; row < height; row++) 24 { 25 sy = (Double)(row + 0.5) * h - 0.5; 26 y = (Int32)sy; 27 if (y > sy) 28 y--; 29 v = sy - y; 30 for (int col = 0; col < width; col++) 31 { 32 var color = new List<Byte>(); 33 sx = (Double)(col + 0.5) * w - 0.5; 34 x = (Int32)sx; 35 if (x > sx) 36 x--; 37 u = sx - x; 38 39 pm[0] = (1.0 - u) * (1.0 - v); 40 pm[1] = v * (1.0 - u); 41 pm[2] = u * (1.0 - v); 42 pm[3] = u * v; 43 44 for (int n = 0; n < 4; n++) 45 { 46 Int32 xx = (n == 0 || n == 1) ? x : x + 1; 47 if (xx < 0) 48 xx = 0; 49 else if (xx > srcImg.Width - 1) 50 xx = srcImg.Width - 1; 51 Int32 yy = (n == 0 || n == 2) ? y : y + 1; 52 if (yy < 0) 53 yy = 0; 54 else if (yy > srcImg.Height - 1) 55 yy = srcImg.Height - 1; 56 57 Byte* srcPoint = (Byte*)srcData.Scan0 + xx * 3 + yy * srcData.Stride; 58 color.Add(srcPoint[0]); 59 color.Add(srcPoint[1]); 60 color.Add(srcPoint[2]); 61 } 62 63 Byte* destPoint = (Byte*)destData.Scan0 + col * 3 + row * destData.Stride; 64 destPoint[0] = (Byte)(pm[0] * color[0] + pm[1] * color[3] + pm[2] * color[6] + pm[3] * color[9]); 65 destPoint[1] = (Byte)(pm[0] * color[1] + pm[1] * color[4] + pm[2] * color[7] + pm[3] * color[10]); 66 destPoint[2] = (Byte)(pm[0] * color[2] + pm[1] * color[5] + pm[2] * color[8] + pm[3] * color[11]); 67 } 68 } 69 70 destImg.UnlockBits(destData); 71 srcImg.UnlockBits(srcData); 72 return destImg; 73 }
效果还是不错滴。额。里边处理的都是24位彩图。用的都是unsafe代码,没感觉到比c++慢多少。。- -