@Sulcan

C#:Почему возникает ArgumentException?

using System;
using System.Drawing;
using System.IO;

namespace MinecraftImage
{
    class ImageReader
    {
        private Image image;
        private int height;
        private int width;
        private int lenght;
        private int newWidth;
        public ImageReader(string path)
        {
            try
            {
                image = new Bitmap(path);
            }
            catch(FileNotFoundException ex)
            {
                Console.WriteLine($"Ошибка: {ex.Message}");
                Console.ReadKey();
                Environment.Exit(1);
            }
            catch(ArgumentException ex)
            {
                Console.WriteLine($"Ошибка: {ex.Message}");
                Console.ReadKey();
                Environment.Exit(1);
            }
            height = image.Height;
            width = image.Width;
            lenght = height * width;
            newWidth = (int)Math.Round((double)(width * (256 / height)));
            Console.WriteLine($"Высота в пикселях:{height}");
            Console.WriteLine($"Длина в пикселях{width}");
            if (height > 256)
            {
                Console.WriteLine("Высота фото больше 256");
                Console.WriteLine("Хотите изменить разрешение? \n y/n да/нет");
                switch (Console.ReadKey().Key)
                {
                    case ConsoleKey.Y:
                        image = new Bitmap(image, newWidth, 256);
                        Console.WriteLine($"Конечное разрешение: {height},{width}");
                        Console.ReadKey();
                        break;
                    case ConsoleKey.N:
                        break;
                    default:
                        Console.WriteLine("Неверная клавиша");
                        Console.WriteLine("Выход из программы");
                        Environment.Exit(1);
                        break;
                }
            }
        }
    }
}

В case ConsoleKey.Y, при инициализации класса bitmap с новым разрешением фото ошибка ArgumentException
  • Вопрос задан
  • 119 просмотров
Пригласить эксперта
Ответы на вопрос 1
MANAB
@MANAB
Разрабатываю на C#: Web, Desktop, Gamedev
Проблема в передаче image в конструкторе Bitmap image = new Bitmap(image, newWidth, 256);
т.к. конструктора Bitmap(Bitmap,Int32,Int32) нету, есть Bitmap(Image,Int32,Int32)
По идее должно заработать так:
image = new Bitmap((System.Drawing.Image)image, newWidth, 256);
Ответ написан
Ваш ответ на вопрос

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

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