后台输出多一个picturebox在新的picturebox1后面 PictureBox newbitmap =new PictureBox();
PictureBox oldbitmap = new PictureBox();
oldbitmap.Location = new Point(newbitmap.Location.X + newbitmap.Width, newbitmap.Location.Y);//跟在第一个PictureBox 后面
this.Controls.Add(oldbitmap);
PictureBox oldbitmap = new PictureBox();
oldbitmap.Location = new Point(newbitmap.Location.X + newbitmap.Width, newbitmap.Location.Y);//跟在第一个PictureBox 后面
this.Controls.Add(oldbitmap);
Bitmap newbitmap = new Bitmap(textBox1.Text);
Bitmap oldbitmap = new Bitmap(textBox2.Text); //上下合并
int iWidth = newbitmap.Width > oldbitmap.Width ? newbitmap.Width : oldbitmap.Width;
int iHeight = newbitmap.Height + oldbitmap.Height;
Bitmap bitmap = new Bitmap(iWidth, iHeight); Graphics g = Graphics.FromImage(bitmap);
g.DrawImage(newbitmap, new Rectangle(0, 0, newbitmap.Width, newbitmap.Height), new Rectangle(0, 0, newbitmap.Width, newbitmap.Height), GraphicsUnit.Pixel);
g.DrawImage(oldbitmap, new Rectangle(0, newbitmap.Height, oldbitmap.Width, oldbitmap.Height), new Rectangle(0, 0, oldbitmap.Width, oldbitmap.Height), GraphicsUnit.Pixel);
g.Dispose(); pictureBox1.Image = bitmap;
你可以把有噪声那一张凡是没有点的像素Alpha分量全部置为0。如果不设置Alpha,那么底色不是透明而是白色,就把下面的图片挡住了。
int Height = this.pictureBox1.Image.Height;
int Width = this.pictureBox1.Image.Width;
Bitmap newbitmap = new Bitmap(Width, Height);
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, newbitmap.Width, newbitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = newbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, newbitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = newbitmap.Width * newbitmap.Height; this.textBox1.Text = (newbitmap.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);
newbitmap.UnlockBits(bmpData);
int iWidth = newbitmap.Width;
int iHeight = newbitmap.Height;
pictureBox1.Image = newbitmap;
你可以把有噪声那一张凡是没有点的像素Alpha分量全部置为0。如果不设置Alpha,那么底色不是透明而是白色,就把下面的图片挡住了。
我代码写在下面,能具体说下如何改么
Bitmap oldbitmap = (Bitmap)this.pictureBox1.Image;
Bitmap newbitmap = oldbitmap;
10楼的方法试过么,你上面的代码创建了一个新位图,然后根据噪声修改像素,再把这个图赋给图片控件,并不是在原图上做这些操作,所以你看到5楼的图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;简单点就直接在原图操作了
10楼的方法试过么,你上面的代码创建了一个新位图,然后根据噪声修改像素,再把这个图赋给图片控件,并不是在原图上做这些操作,所以你看到5楼的图
简单点就直接在原图操作了
谢谢你,这样就成功了。 还有个问题就是为什么只有一半的图像有噪声?