写了一个C#实现图像凸透镜效果的小程序,有图有源码,给大家分享一下,代码尚未优化,原理清晰,图像好玩,让你做个快乐的程序员哈哈!
先看原图--效果图:
免费下载链接:
http://download.csdn.net/detail/trent1985/5027453源代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;namespace IceFilter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
srcBitmap = new Bitmap("src.jpg");
pictureBox1.Image = (Image)srcBitmap;
}
private static Bitmap srcBitmap = null;
private static int count = 0;
private static Bitmap ConvexFilterProcess(Bitmap srcBitmap, int radius, int cenX, int cenY)
{
Bitmap a = new Bitmap(srcBitmap);
int w = a.Width;
int h = a.Height;
double distance = 0.0;
double dis = 0.0;
if (radius > cenX || radius > cenY)
{
radius = Math.Min(cenX, cenY);
}
Bitmap dst = new Bitmap(w, h);
System.Drawing.Imaging.BitmapData srcData = a.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapData dstData = dst.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* pOut = (byte*)dstData.Scan0.ToPointer();
byte* p = (byte*)srcData.Scan0.ToPointer();
int sWidth = srcData.Stride;
int stride = sWidth - w * 3;
int R, G, B;
int newX = 0, newY = 0;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
B = pIn[0];
G = pIn[1];
R = pIn[2];
distance = (x - cenX) * (x - cenX) + (y - cenY) * (y - cenY);
dis = Math.Sqrt(distance);
if (distance <= radius * radius&&distance>0)
{
newX = (int)Math.Floor(dis * (double)(x - cenX) / (double)radius + (double)cenX);
newY = (int)Math.Floor(dis * (double)(y - cenY) / (double)radius + (double)cenY);
pOut[0] = (byte)(*(p + newX * 3 + newY * srcData.Stride));
pOut[1] = (byte)(*(p + newX * 3 + 1 + newY * srcData.Stride));
pOut[2] = (byte)(*(p + newX * 3 + 2 + newY * srcData.Stride));
}
else
{
pOut[0] = (byte)B;
pOut[1] = (byte)G;
pOut[2] = (byte)R;
}
pIn += 3;
pOut += 3;
}
pIn += stride;
pOut += stride;
}
a.UnlockBits(srcData);
dst.UnlockBits(dstData);
}
return dst; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(e.X<srcBitmap.Width&&e.X>0&&e.Y<srcBitmap.Height&&e.Y>0)
{
pictureBox1.Image = (Image)ConvexFilterProcess(srcBitmap, 50, e.X, e.Y);
} } private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
pictureBox1.Image.Save(count.ToString() + ".jpg");
count++;
}
}
}注意:鼠标移动过程中点击右键,即实现图像的保存,保存路径在bin文件夹内c#image凸透镜
先看原图--效果图:
免费下载链接:
http://download.csdn.net/detail/trent1985/5027453源代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;namespace IceFilter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
srcBitmap = new Bitmap("src.jpg");
pictureBox1.Image = (Image)srcBitmap;
}
private static Bitmap srcBitmap = null;
private static int count = 0;
private static Bitmap ConvexFilterProcess(Bitmap srcBitmap, int radius, int cenX, int cenY)
{
Bitmap a = new Bitmap(srcBitmap);
int w = a.Width;
int h = a.Height;
double distance = 0.0;
double dis = 0.0;
if (radius > cenX || radius > cenY)
{
radius = Math.Min(cenX, cenY);
}
Bitmap dst = new Bitmap(w, h);
System.Drawing.Imaging.BitmapData srcData = a.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapData dstData = dst.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* pOut = (byte*)dstData.Scan0.ToPointer();
byte* p = (byte*)srcData.Scan0.ToPointer();
int sWidth = srcData.Stride;
int stride = sWidth - w * 3;
int R, G, B;
int newX = 0, newY = 0;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
B = pIn[0];
G = pIn[1];
R = pIn[2];
distance = (x - cenX) * (x - cenX) + (y - cenY) * (y - cenY);
dis = Math.Sqrt(distance);
if (distance <= radius * radius&&distance>0)
{
newX = (int)Math.Floor(dis * (double)(x - cenX) / (double)radius + (double)cenX);
newY = (int)Math.Floor(dis * (double)(y - cenY) / (double)radius + (double)cenY);
pOut[0] = (byte)(*(p + newX * 3 + newY * srcData.Stride));
pOut[1] = (byte)(*(p + newX * 3 + 1 + newY * srcData.Stride));
pOut[2] = (byte)(*(p + newX * 3 + 2 + newY * srcData.Stride));
}
else
{
pOut[0] = (byte)B;
pOut[1] = (byte)G;
pOut[2] = (byte)R;
}
pIn += 3;
pOut += 3;
}
pIn += stride;
pOut += stride;
}
a.UnlockBits(srcData);
dst.UnlockBits(dstData);
}
return dst; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(e.X<srcBitmap.Width&&e.X>0&&e.Y<srcBitmap.Height&&e.Y>0)
{
pictureBox1.Image = (Image)ConvexFilterProcess(srcBitmap, 50, e.X, e.Y);
} } private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
pictureBox1.Image.Save(count.ToString() + ".jpg");
count++;
}
}
}注意:鼠标移动过程中点击右键,即实现图像的保存,保存路径在bin文件夹内c#image凸透镜
解决方案 »
- FtpWebRequest操作FTP
- ckfinder 设置问题
- unit test 的时候遇到 HttpContext.Current.Session
- C# 关于IList<T>
- 如何高效的将excel导入sqlserver [共享]+[散分]
- 疑难问题~~,请各位高手指点~~!可追加分!
- lock(typeof(myClass)){} 这句代码对我的myClass有哪些操作和影响呢?在线等
- 有谁知道怎么接收条码扫描仪返回的结果
- 最近公司要求写一个能在WPF上阅读PDF,但是我在开发途中遇到了一些问题。
- 什麽是阻塞模式和非阻塞模式啊?
- //-----------------------------------------------------------------浏览器问题.求高手解决!!
- 基于Easysl 架构的Silverlight应用程序部署在IIS7.0上之后出现错误
我们到2月8号放假。 你这种滤镜paint.net算法里有不少,很简单的坐标映射 ,加上双线性插值就看可以抗锯齿了。
cw277346909064
oppo11
等级:
#79 得分:0 回复于: 2013-01-26 11:11:58
学习~~参考~~谢谢~~~~~~~