110,535
社区成员
发帖
与我相关
我的任务
分享
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++;
}
}
}