GIF图像转换为2D字节数组

时间:2017-12-17 10:52:10

标签: c# arrays image gif dct

我打开GIF图像并使用N参数我决定哪些帧是关键帧而其他帧不是。然后我想将关键帧分成8 * 8块并对它们执行DCT。我的扩展是当我将每个关键帧转换为字节数组时,它们都是不同的大小。

例如:

  • 宽度/高度:250/250
  • 17frames
  • 6键帧....第一个和N = 3所以每个第三个
  • 其他11个框架
  • 关键帧字节[]大小:19049,19417,18828,18904,19572,18782

现在我怎么能将这个等分为二维数组以及如何存在差异。我错过了一些关于GIF格式的内容吗?

Image[] key_frames;
Image[] other_frames;
void getFrames(Image originalImg)
{
    int N = Int32.Parse(textBox1.Text);
    int size;
    int numberOfFrames = originalImg.GetFrameCount(FrameDimension.Time);
    if (N == 1)
        size = numberOfFrames / 2;
    else
        size = numberOfFrames / N + 1;

    key_frames = new Image[size];
    other_frames = new Image[numberOfFrames-size];

    int counter = 0,array_counter=0, array_counter_others=0;

    for (int i = 0; i < numberOfFrames; i++)
    {
        originalImg.SelectActiveFrame(FrameDimension.Time, i);
        if (i == 0 || counter == N-1)
        {
            key_frames[array_counter] = ((Image)originalImg.Clone());
            counter = 0;
            array_counter++;
            if (N == 1)
                counter--;
        }
        else
        {
            other_frames[array_counter_others]= ((Image)originalImg.Clone());
            counter++;
            array_counter_others++;
        }
    }
}

public byte[] imageToByteArray(Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}

private void button1_Click(object sender, EventArgs e)
{
    string file = "";
    DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK)
        file = openFileDialog1.FileName;

    Image img = Image.FromFile(file);
    int width = img.Width;
    int height = img.Height; 
    pictureBox1.Width = width;
    pictureBox1.Height = height;
    pictureBox1.Image = img;

    getFrames(img);

    Dictionary<int, byte[]> byte_key_frames = new Dictionary<int, byte[]>();
    Dictionary<int, byte[]> byte_other_frames = new Dictionary<int, byte[]>();

    int counter = 0;
    foreach(Image frame in key_frames)
    {
        byte_key_frames.Add(counter, imageToByteArray(key_frames[counter]));
        counter++;
    }
    counter = 0;
    foreach (Image frame in other_frames)
    {
        byte_other_frames.Add(counter, imageToByteArray(other_frames[counter]));
        counter++;
    }
}

0 个答案:

没有答案