.NET 图片双线性插值缩放算法
2013-03-28 10:44
127 查看
不放首页,都木有人看的说。。。
之前公司搞图像处理,代码都拷不出来,只剩下一点点自己平时找的和写的了。
代码全是自己写的,原理嘛,,都是从c++,java里边学来的。
效果还是不错滴。额。里边处理的都是24位彩图。用的都是unsafe代码,没感觉到比c++慢多少。。- -
之前公司搞图像处理,代码都拷不出来,只剩下一点点自己平时找的和写的了。
代码全是自己写的,原理嘛,,都是从c++,java里边学来的。
/// <summary> /// 双线性插值缩放算法 /// </summary> /// <param name="srcImg"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static Image Thumbnail(Bitmap srcImg, Int32 width, Int32 height) { var srcData = srcImg.LockBits(new Rectangle(0, 0, srcImg.Width, srcImg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); var destImg = new Bitmap(width, height, PixelFormat.Format24bppRgb); var destData = destImg.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); Int32 x, y; Double sx, sy, u, v; var pm = new Double[4]; Double w = (Double)srcImg.Width / (Double)width; Double h = (Double)srcImg.Height / (Double)height; for (int row = 0; row < height; row++) { sy = (Double)(row + 0.5) * h - 0.5; y = (Int32)sy; if (y > sy) y--; v = sy - y; for (int col = 0; col < width; col++) { var color = new List<Byte>(); sx = (Double)(col + 0.5) * w - 0.5; x = (Int32)sx; if (x > sx) x--; u = sx - x; pm[0] = (1.0 - u) * (1.0 - v); pm[1] = v * (1.0 - u); pm[2] = u * (1.0 - v); pm[3] = u * v; for (int n = 0; n < 4; n++) { Int32 xx = (n == 0 || n == 1) ? x : x + 1; if (xx < 0) xx = 0; else if (xx > srcImg.Width - 1) xx = srcImg.Width - 1; Int32 yy = (n == 0 || n == 2) ? y : y + 1; if (yy < 0) yy = 0; else if (yy > srcImg.Height - 1) yy = srcImg.Height - 1; Byte* srcPoint = (Byte*)srcData.Scan0 + xx * 3 + yy * srcData.Stride; color.Add(srcPoint[0]); color.Add(srcPoint[1]); color.Add(srcPoint[2]); } Byte* destPoint = (Byte*)destData.Scan0 + col * 3 + row * destData.Stride; destPoint[0] = (Byte)(pm[0] * color[0] + pm[1] * color[3] + pm[2] * color[6] + pm[3] * color[9]); destPoint[1] = (Byte)(pm[0] * color[1] + pm[1] * color[4] + pm[2] * color[7] + pm[3] * color[10]); destPoint[2] = (Byte)(pm[0] * color[2] + pm[1] * color[5] + pm[2] * color[8] + pm[3] * color[11]); } } destImg.UnlockBits(destData); srcImg.UnlockBits(srcData); return destImg; }
效果还是不错滴。额。里边处理的都是24位彩图。用的都是unsafe代码,没感觉到比c++慢多少。。- -
相关文章推荐
- ASP.NET实现图片防盗链(转)
- ASP.NET中存取图片到数据库的示例(转载http://www.knowsky.com/3257.html)
- Asp.Net Core 通过自定义中间件防止图片盗链的实例(转)
- asp.net上传图片生成缩略图
- vb.net借助剪贴板将图片导入excel内
- 【读书笔记】Asp.Net MVC 上传图片到数据库(会的绕行)
- ASP.NET(C#)实现一次性上传多张图片(多个文件)
- ASP.NET 给图片动态添加文字水印
- 在ASP.NET中存取图片到数据库的示例
- [转]FCKeditor上传图片提示“没有权限”的解决方法(.net)
- html5 上传图片.net实现
- asp.net中在后台更换控件图片的代码
- ASP.NET中检测图片真实否防范病毒上传
- ASP.NET MVC应用程序把文字写在图片上
- ASP.NET MVC图片管理(上传,预览与显示)
- 在asp.net生成的word文档中插入图片
- ASP.NET实现自适应图片大小的弹出窗口
- ASP.NET MVC编程入门--图片水印与缩略图(一)
- ASP.NET下如何上传图片到数据库,并且读出图片
- C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte