写了一个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凸透镜