14

.gif 画像をステップ実行して、各ピクセルの RGB 値、x 座標と y 座標を決定する必要があります。誰かがこれを達成する方法の概要を教えてもらえますか? (方法論、使用する名前空間など)

4

3 に答える 3

27

これは、LockBits() と GetPixel() を使用した両方のメソッドの完全な例です。LockBits() の信頼性の問題に加えて、物事は簡単に複雑になります。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace BitmapReader
{
    class Program
    {
        static void Main(string[] args)
        {
            //Try a small pic to be able to compare output, 
            //a big one to compare performance
            System.Drawing.Bitmap b = new 
                System.Drawing.Bitmap(@"C:\Users\vinko\Pictures\Dibujo2.jpg"); 
            doSomethingWithBitmapSlow(b);
            doSomethingWithBitmapFast(b);
        }

        public static void doSomethingWithBitmapSlow(System.Drawing.Bitmap bmp)
        {
            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    Color clr = bmp.GetPixel(x, y);
                    int red = clr.R;
                    int green = clr.G;
                    int blue = clr.B;
                    Console.WriteLine("Slow: " + red + " " 
                                       + green + " " + blue);
                }
            }
        }

        public static void doSomethingWithBitmapFast(System.Drawing.Bitmap bmp)
        {
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

            System.Drawing.Imaging.BitmapData bmpData =
                bmp.LockBits(rect, 
                    System.Drawing.Imaging.ImageLockMode.ReadOnly,
                    bmp.PixelFormat);

            IntPtr ptr = bmpData.Scan0;

            int bytes = bmpData.Stride * bmp.Height;
            byte[] rgbValues = new byte[bytes];

            System.Runtime.InteropServices.Marshal.Copy(ptr, 
                           rgbValues, 0, bytes);

            byte red = 0;
            byte green = 0;
            byte blue = 0;

            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    //See the link above for an explanation 
                    //of this calculation
                    int position = (y * bmpData.Stride) + (x * Image.GetPixelFormatSize(bmpData.PixelFormat)/8); 
                    blue = rgbValues[position];
                    green = rgbValues[position + 1];
                    red = rgbValues[position + 2];
                    Console.WriteLine("Fast: " + red + " " 
                                       + green + " " + blue);
                }
            }
            bmp.UnlockBits(bmpData);
        }
    }
}
于 2009-08-04T22:20:25.727 に答える
9

を使用して画像をロードし、繰り返しnew Bitmap(filename)使用できます。Bitmap.GetPixelこれは非常に遅いですが、単純です。(例については、Vinko の回答を参照してください。)

パフォーマンスが重要な場合はBitmap.LockBits、安全でないコードを使用することをお勧めします。明らかに、これによりソリューションを使用できる場所の数が減少し (信頼レベルの観点から)、一般的にはより複雑になりますが、はるかに高速になる可能性があります。

于 2009-08-04T22:22:02.543 に答える
0

GIF がアニメーション化されていない場合は、次を使用します。

Image img = Image.FromFile("image.gif");

for (int x = 0; x < img.Width; x++)
{
    for (int y = 0; y < img.Height; y++)
    {
        // Do stuff here
    }
}

(未テスト)


それ以外の場合は、これを使用してすべてのフレームをループします。

Image img = Image.FromFile("animation.gif");

FrameDimension frameDimension = new FrameDimension(img.FrameDimensionsList[0]);
int frames = img.GetFrameCount(frameDimension);

for (int f = 0; f < frames; f++)
{
    img.SelectActiveFrame(frameDimension, f);

    for (int x = 0; x < img.Width; x++)
    {
        for (int y = 0; y < img.Height; y++)
        {
            // Do stuff here
        }
    }
}

(未テスト)

于 2019-07-22T05:55:13.053 に答える