C# 如何向图像添加噪声或将其转换为24bpp?

C# 如何向图像添加噪声或将其转换为24bpp?,c#,bitmap,noise,C#,Bitmap,Noise,诺布需要帮助! 我有一个图像,我需要在上面添加噪声。我尝试使用一个RGE libs来做这件事,但这种方法只适用于24bpp位图,调整大小后我得到了一些不同的结果。问题是如何将位图转换为24bpp或如何在其上添加噪波?也许有一些LIB可以让这更容易 调整大小: private Image Fit(Image image) { Image img = image; if (filepath != null) { if (img.Width > pictu

诺布需要帮助! 我有一个图像,我需要在上面添加噪声。我尝试使用一个RGE libs来做这件事,但这种方法只适用于24bpp位图,调整大小后我得到了一些不同的结果。问题是如何将位图转换为24bpp或如何在其上添加噪波?也许有一些LIB可以让这更容易

调整大小:

private Image Fit(Image image)
{
    Image img = image;
    if (filepath != null)
    {
        if (img.Width > pictureBox1.Width)
        {
            double op = ((pictureBox1.Width - (pictureBox1.Width % 100)) % 100) + (pictureBox1.Width % 100) * 0.01;
            double percent = img.Width / (pictureBox1.Width * 0.01);
            double temp = ((percent - percent % 100 + 100) - percent) * pictureBox1.Height * 0.01;

            double height = pictureBox1.Height * 0.01 * ((percent - percent % 100 + 100) - percent);

            System.Drawing.Size sz = new Size(pictureBox1.Width, (int)height);
            img = resizeImage(img, sz);
        }
        if (img.Height > pictureBox1.Height)
        {
            double percent = img.Height / (pictureBox1.Height * 0.01);
            double temp = ((percent - percent % 100 + 100) - percent) * pictureBox1.Width * 0.01;

            double width = pictureBox1.Width * 0.01 * ((percent - percent % 100 + 100) - percent);

            System.Drawing.Size sz = new Size((int)width, pictureBox1.Height);
            img = resizeImage(img, sz);
        }
    }
    return img;
}

我有一种错误-系统完全拒绝用1除以100,所以我必须用1乘以0.01,否则我得到0。

没有发现任何好的结果。我就是这样解决的:

public void GenerateNoise(Image img, int intense)
        {
            Bitmap finalBmp = img as Bitmap;
            Random r = new Random();
            int width = img.Width;
            int height = img.Height;
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int def = r.Next(0, 100);
                    if (def < intense)
                    {
                        int op = r.Next(0, 1);
                        if (op == 0)
                        {
                            int num = r.Next(0, intense);
                            Color clr = finalBmp.GetPixel(x, y);
                            int R = (clr.R + clr.R + num)/2;
                            if (R > 255) R = 255;
                            int G = (clr.G + clr.G + num) / 2;
                            if (G > 255) G = 255;
                            int B = (clr.B + clr.B + num) / 2;
                            if (B > 255) B = 255;
                            Color result = Color.FromArgb(255, R, G, B);
                            finalBmp.SetPixel(x, y, result);
                        }
                        else
                        {
                            int num = r.Next(0, intense);
                            Color clr = finalBmp.GetPixel(x, y);
                            Color result = Color.FromArgb(255, (clr.R + clr.R - num) / 2, (clr.G + clr.G - num) / 2,
                                (clr.B + clr.B - num) / 2);
                            finalBmp.SetPixel(x, y, result);
                        }
                    }
                }
            }

        }
public void GenerateNoise(图像img,int-sensitive)
{
位图finalBmp=img作为位图;
随机r=新随机();
内部宽度=内部宽度;
内部高度=内部高度;
对于(int x=0;x255)R=255;
int G=(clr.G+clr.G+num)/2;
如果(G>255)G=255;
intb=(clr.B+clr.B+num)/2;
如果(B>255)B=255;
颜色结果=颜色。来自argb(255,R,G,B);
最终设置像素(x,y,结果);
}
其他的
{
int num=r.Next(0,强度);
颜色clr=finalBmp.GetPixel(x,y);
颜色结果=Color.FromArgb(255,(clr.R+clr.R-num)/2,(clr.G+clr.G-num)/2,
(clr.B+clr.B-num)/2);
最终设置像素(x,y,结果);
}
}
}
}
}

调整图像大小以保持纵横比非常容易。计算水平和垂直比例因子,然后选择其中最小的一个

double vscale = 1.0;
double hscale = 1.0;
if (img.Width > pictureBox1.Width)
{
    hscale = (double)pictureBox1.Width/img.Width;
}
if (img.Height > pictureBox1.Height)
{
    vscale = (double)pictureBox1.Height/img.Height;
}
double scale = Math.Min(hscale, vscale);
double width = scale * img.Width;
double height = scale * img.Height;

Size sz = new Size((int)width, (int)height);
img = resizeImage(img, sz)

请注意,仅当图像大于长方体时,此选项才会缩放。如果图像小于长方体,它将不会缩放图像以使其适合长方体。

你确定你的1除以100错误不是一个整数数学问题吗?1/100=0,但1.0/100,1/100.0,(双)1/100等。所有=0.01每当我做同样的事情时,我得到0.01。这是它第一次不同。因此,您的问题是无法将图像转换为24 bpp,或者您的大小调整不起作用?在托管代码中自己进行调整会很慢,并且可能不会得到最好的结果。您应该解决的真正问题是,为什么在调整大小时无法获得正确的像素格式。如何调整大小?密码?向我们展示调整大小的代码!