How can I iterate through each pixel in a .gif image?

前端 未结 3 1495
礼貌的吻别
礼貌的吻别 2020-12-09 05:04

I need to step through a .gif image and determine the RGB value of each pixel, x and y coordinates. Can someone give me an overview of how I can accomplish this? (methodolo

3条回答
  •  一向
    一向 (楼主)
    2020-12-09 05:20

    If your gif isn't animated use this:

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

    (Untested)


    Otherwise use this to loop through all the frames, as well:

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

    (Untested)

提交回复
热议问题