S
S
Sulcan2021-06-17 12:14:54
.NET
Sulcan, 2021-06-17 12:14:54

C#: Why is ArgumentException thrown?

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

In case ConsoleKey.Y, when initializing the bitmap class with a new photo resolution, the ArgumentException error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Kachan, 2021-06-17
@MANAB

The problem is passing image in the Bitmap constructor . there is no Bitmap(Bitmap,Int32,Int32) constructor, there is Bitmap(Image,Int32,Int32) In theory, it should work like this:image = new Bitmap(image, newWidth, 256);

image = new Bitmap((System.Drawing.Image)image, newWidth, 256);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question