@marlaaa

Не удалось обнаружить компонент обработки изображений, который подходит для завершения данной операции. Почему?

Цель моей программы это открыть файл формата bmp(а в дальнейшем и моего кастомного формата) и вывести на экран, в чем моя ошибка, что я могу почитать на эту тему?
namespace mSystem
{
    public struct FileHeader
    {
        public ushort bfType;
        public uint bfSize;
        public ushort bfReserved1;
        public ushort bfReserved2;
        public uint bfOffBits;
    }

    public struct InfoHeader
    {
        public uint biSize;
        public int biWidth;
        public int biHeight;
        public ushort biPlanes;
        public ushort biBitCount;
        public uint biCompression;
        public uint biSizeImage;
        public int biXPelsPerMeter;
        public int biYPelsPerMeter;
        public uint biClrUsed;
        public uint biClrImportant;
    }

    public struct Color
    {
        public byte blue;
        public byte green;
        public byte red;
    }

    internal class ImageOperation
    {
        public Color[] srcImage;
        public Color[] dstImage;
        public int width;
        public int height;
        public int pixelSize;
        FileHeader fileHeader = new FileHeader();
        InfoHeader infoHeader = new InfoHeader();

        public byte[] OpenFile(string filepath)
        {
            using (var file = new FileStream(filepath, FileMode.Open, FileAccess.Read))
            {
                var reader = new BinaryReader(file);
                fileHeader.bfType = reader.ReadUInt16();
                fileHeader.bfSize = reader.ReadUInt32();
                fileHeader.bfReserved1 = reader.ReadUInt16();
                fileHeader.bfReserved2 = reader.ReadUInt16();
                fileHeader.bfOffBits = reader.ReadUInt32();

                infoHeader.biSize = reader.ReadUInt32();
                infoHeader.biWidth = reader.ReadInt32();
                infoHeader.biHeight = reader.ReadInt32();
                infoHeader.biPlanes = reader.ReadUInt16();
                infoHeader.biBitCount = reader.ReadUInt16();
                infoHeader.biCompression = reader.ReadUInt32();
                infoHeader.biSizeImage = reader.ReadUInt32();
                infoHeader.biXPelsPerMeter = reader.ReadInt32();
                infoHeader.biYPelsPerMeter = reader.ReadInt32();
                infoHeader.biClrUsed = reader.ReadUInt32();
                infoHeader.biClrImportant = reader.ReadUInt32();

                width = infoHeader.biWidth;
                height = infoHeader.biHeight;

                srcImage = new Color[width * height];
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        srcImage[i * width + j].blue = reader.ReadByte();
                        srcImage[i * width + j].green = reader.ReadByte();
                        srcImage[i * width + j].red = reader.ReadByte();
                    }
                }
                byte[] byteArray = new byte[srcImage.Length * 3];
                int index = 0;

                foreach (Color color in srcImage)
                {
                    byteArray[index++] = color.red;
                    byteArray[index++] = color.green;
                    byteArray[index++] = color.blue;
                }
                return byteArray;
            }
        }
    }

}

private void OpenNewFile_Click(object sender, RoutedEventArgs e)
        {
            var ofd = new OpenFileDialog();
            ofd.Filter = "Image files (*.bmp;*.vmv)|*.bmp;*.vmv|All files (*.*)|*.*";
            if (ofd.ShowDialog() == true)
            {
                filePath = ofd.FileName;
                var imageOperation = new ImageOperation();
                byte[] imageBytes = imageOperation.OpenFile(filePath);

                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = new MemoryStream(imageBytes);
                bitmapImage.EndInit(); //исключение тут
            }
        }
  • Вопрос задан
  • 108 просмотров
Пригласить эксперта
Ответы на вопрос 1
mindtester
@mindtester Куратор тега C#
http://iczin.su/hexagram_48
byte[] imageBytes = imageOperation.OpenFile(filePath);
что то смущает.. OpenFile точно делает чтение? иначе вы можете действительно получить пустой стрим...

ps если под виндой и в студии, отладчик вам в помощь ;))
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы