您的位置:首页 > 编程语言 > C#

【图像处理】C#实现哈哈镜效果

2017-02-10 11:34 826 查看
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;
using System.Drawing.Imaging;

namespace DistortingMirror
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
srcBitmap = new Bitmap("1.jpg");
pictureBox1.Image = (Image)srcBitmap;
pictureBox1.Width = srcBitmap.Width;
pictureBox1.Height = srcBitmap.Height;
}

private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private Bitmap srcBitmap;
public static int[] GetData(Bitmap a)
{
try
{
int w = a.Width;
int h = a.Height;
int[] sData = new int[w * h * 3];
Bitmap dstBitmap = new Bitmap(a.Width, a.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapData srcData = a.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* pOut = (byte*)dstData.Scan0.ToPointer();
byte* p;
int stride = srcData.Stride;
int r, g, b;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
p = pIn;
r = p[2];
g = p[1];
b = p[0];
sData[x * 3 + y * w * 3] = b;
sData[x * 3 + 1 + y * w * 3] = g;
sData[x * 3 + 2 + y * w * 3] = r;
pIn += 3;
pOut += 3;
}
pIn += srcData.Stride - w * 3;
pOut += srcData.Stride - w * 3;
}
a.UnlockBits(srcData);
dstBitmap.UnlockBits(dstData);
return sData;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
return null;
}
}

public static Bitmap Rebuildimage(int[] data, int w, int h)
{
try
{
Bitmap dstBitmap = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
unsafe
{
byte* pOut = (byte*)dstData.Scan0.ToPointer();
int stride = dstData.Stride;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
pOut[0] = (byte)data[x * 3 + y * w * 3];
pOut[1] = (byte)data[x * 3 + 1 + y * w * 3];
pOut[2] = (byte)data[x * 3 + 2 + y * w * 3];

pOut += 3;
}
pOut += dstData.Stride - w * 3;
}
dstBitmap.UnlockBits(dstData);
return dstBitmap;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
return null;
}
}
public static int[] FunMirror(int[] srcData, double factor, int w, int h, int x, int y)
{
int cenX = x;
int cenY = y;
int newX = 0;
int newY = 0;
int offsetX = 0;
int offsetY = 0;
int radius = 0;
double theta = 0;
int[] tempData = (int[])srcData.Clone();
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
int tX = i - cenX;
int tY = j - cenY;
theta = Math.Atan2((double)tY, (double)tX);
radius = (int)Math.Sqrt((double)(tX * tX + tY * tY));
int newR = (int)(Math.Sqrt((double)radius) * 12);
newX = cenX + (int)(newR * Math.Cos(theta));
newY = cenY + (int)(newR * Math.Sin(theta));
if (newX > 0 && newX < w)
{
offsetX = newX;
}
else
{
newX = 0;
}
if (newY > 0 && newY < h)
{
offsetY = newY;
}
else
{
newY = 0;
}
tempData[i * 3 + j * w * 3] = srcData[newX * 3 + newY * w * 3];
tempData[i * 3 + 1 + j * w * 3] = srcData[newX * 3 + 1 + newY * w * 3];
tempData[i * 3 + 2 + j * w * 3] = srcData[newX * 3 + 2 + newY * w * 3];
}
}
return tempData;

}
public static Bitmap DistortingMirror(Bitmap src, double degree, int x, int y)
{
int[] data = GetData(src);
int w = src.Width;
int h = src.Height;
int[] dstData = FunMirror(data, degree, w, h, x, y);
Bitmap dst = Rebuildimage(dstData, w, h);
return dst;
}

private void timer1_Tick(object sender, EventArgs e)
{
Random xV = new Random();
Random yV = new Random();
int x = xV.Next(pictureBox1.Image.Width) + 1;
int y = yV.Next(pictureBox1.Image.Height) + 1;
pictureBox1.Image = (Image)DistortingMirror(srcBitmap, 13, x, y);

}

}
}



  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: