0

1つの画像のすべてのピクセルを通過し、元の画像のピクセルの色をピクセル数で割った新しいビットマップを作成して平均色を作成することにより、C#で画像をぼかしようとしています。実行しても何も起こりません。コードは次のとおりです。

private void blurToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Bitmap img = new Bitmap(pictureBox1.Image);
        Bitmap blurPic = new Bitmap(img.Width, img.Height);

        Int32 avgR = 0, avgG = 0, avgB = 0;
        Int32 blurPixelCount = 0;

        for (int y = 0; y < img.Height; y++)
        {
            for (int x = 0; x < img.Width; x++)
            {
                Color pixel = img.GetPixel(x, y);
                avgR += pixel.R;
                avgG += pixel.G;
                avgB += pixel.B;

                blurPixelCount++;
            }
        }

        avgR = avgR / blurPixelCount;
        avgG = avgG / blurPixelCount;
        avgB = avgB / blurPixelCount;

        for (int y = 0; y < img.Height; y++)
        {
            for (int x = 0; x < img.Width; x++)
            {
                blurPic.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB));
            }
        }

        img = blurPic;
    }

ありがとう!

4

2 に答える 2

1

pictureBox1.Image = blurPic;メソッドの最後に使用します。

于 2013-09-25T16:59:08.317 に答える
0

これは、C# コードで画像をぼかす最も簡単な方法ですが、作業を開始できるように、画像が完全に読み込まれるまで待つ必要があります。

つまり、画像に ImageOpened イベント ハンドラーを使用します。そのため、画像をぼかしたり、あらゆる種類の効果を画像に適用したりするためのメソッドを持つ WriteableBitmap クラスを使用します。

WriteableBitExtensions には、これから使用する GaussianBlur5x5 が含まれています。

このコードは、URL から画像を取得していることを前提としているため、最初に画像をダウンロードして、Web からストリームのコンテンツを取得します。ただし、画像がローカルの場合は、最初にそれを IRandomAccessStream に変換して、引数として渡すことができます。

double height = pictureBox1.ActualHeight;
        double width = pictureBox1.ActualWidth;

        HttpClient client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync("image-url-goes-here");

        var webResponse = response.Content.ReadAsStreamAsync();
        randomStream = webResponse.Result.AsRandomAccessStream();
        randomStream.Seek(0);


        wb = wb.Crop(50, 50, 400, 400);
        wb = wb.Resize(10,10 ,         WriteableBitmapExtensions.Interpolation.NearestNeighbor);
        wb = WriteableBitmapExtensions.Convolute(wb,WriteableBitmapExtensions.KernelGaussianBlur5x5);

        pictureBox1.Source= wb;
于 2016-08-17T09:10:10.577 に答える