简体   繁体   中英

How to add noise to image or convert it to 24bpp?

Noob needs help! I have an image and I need to add noise on it. I tried using AForge libs to do it but this method works only with 24bpp bitmaps and I get something different after resizing. The question is how to convert a bitmap to 24bpp or how to add noise on it? Maybe there are some libs for making this easier.

Resizing:

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;
}

PS> I have a type of bug - system totally refuses to divide 1 by 100 so I had to multiply 1 by 0.01 or I get 0.

Resizing the image to maintain aspect ratio is pretty easy. You compute the horizontal and vertical scaling factors, and then select the smallest of the two.

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)

Note that this only scales if the image is larger than the box. It won't zoom the image to make it fit the box if the image is smaller than the box.

Haven't found anything good. That's how I've solved it:

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);
                        }
                    }
                }
            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM