c#怎么让两张图片叠加
发布网友
发布时间:2024-03-24 05:46
我来回答
共1个回答
热心网友
时间:2024-04-08 21:46
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
Bitmap oldbitmap = (Bitmap)this.pictureBox1.Image;
//this.textBox1.Text = (newbitmap.Width.ToString());
//this.textBox2.Text = (newbitmap.Height.ToString());
if (pictureBox1 != null)
{
Form2 noise = new Form2();
if (noise.ShowDialog() == DialogResult.OK)
{
Rectangle rect = new Rectangle(0, 0, oldbitmap.Width, oldbitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = oldbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, oldbitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = oldbitmap.Width * oldbitmap.Height;
this.textBox1.Text = (oldbitmap.Width.ToString());
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
double temp = 0;
byte flagNoise = noise.GetFlag;
double[] paraNoise = new double[2];
paraNoise = noise.GetParaN;
Random r1, r2;
double v1, v2;
r1 = new Random(unchecked((int)DateTime.Now.Ticks));
r2 = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < bytes; i++)
{
switch (flagNoise)
{
case 0:
do
{
v1 = r1.NextDouble();
}
while (v1 <= 0.00000000001);
v2 = r2.NextDouble();
temp = Math.Sqrt(-2 * Math.Log(v1)) * Math.Cos(2 * Math.PI * v2) * paraNoise[1] + paraNoise[0];
break;
case 1:
v1 = r1.NextDouble();
if (v1 <= paraNoise[0])
temp = -500;
else if (v1 >= (1 - paraNoise[1]))
temp = 500;
else
temp = 0;
break;
default:
MessageBox.Show("无效!");
break;
}
temp = temp + grayValues[i];
if (temp > 255)
{
grayValues[i] = 255;
}
else if (temp < 0)
{
grayValues[i] = 0;
}
else
grayValues[i] = Convert.ToByte(temp);
}
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
oldbitmap.UnlockBits(bmpData);
int iWidth = oldbitmap.Width;
int iHeight = oldbitmap.Height;
pictureBox1.Image = oldbitmap;