是否可以使用 switch to select ImageFormat?

Is it possible to use switch to select ImageFormat?

我将图像从一种格式转换为另一种格式,并希望使用该开关处理不同的格式。 System.Drawing.Imaging.ImageFormat 是 public 密封的 class 并且开关无法使用它。 下面的代码有效,但我想在这里使用开关。你能帮我提点建议吗?

public static void ConvertImageFormat(Image image, string targetImageFilePath, int newWidth, int newHeight, ImageFormat imageFormatToConvert)
    {
        using (Bitmap bitmap = new Bitmap(image, newWidth, newHeight))
        {
            using (Graphics graphic = Graphics.FromImage(bitmap))
            {
                graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphic.SmoothingMode = SmoothingMode.HighQuality;
                graphic.CompositingQuality = CompositingQuality.HighQuality;
                graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;

                graphic.DrawImage(image, 0, 0, newWidth, newHeight);

                if (Equals(ImageFormat.Png, imageFormatToConvert))
                {
                    bitmap.Save(targetImageFilePath, ImageFormat.Png);
                }
                else if (Equals(ImageFormat.Gif, imageFormatToConvert))
                {
                    bitmap.Save(targetImageFilePath, ImageFormat.Gif);
                }
                else if (Equals(ImageFormat.Jpeg, imageFormatToConvert))
                {
                    ImageCodecInfo[] arrImageCodecInfo = ImageCodecInfo.GetImageEncoders();
                    using (EncoderParameters encoderParameters = new EncoderParameters(1))
                    {
                        encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
                        bitmap.Save(targetImageFilePath, arrImageCodecInfo[1], encoderParameters);
                    }
                }
                else
                {
                    throw new Exception($"Convert to <{imageFormatToConvert.ToString()}> from " +
                                        $"<{new ImageFormatConverter().ConvertToString(image.RawFormat)}>" +
                                        $" image format is not supported now.");
                }
            }
        }
    }

不,您不能将 Switch 与 ImageFormat 一起使用,因为它是 class。

In C# 6, the match expression must be an expression that returns a value of the following types:

a char.
a string.
a bool.
an integral value, such as an int or a long.
an enum value.

Starting with C# 7, the match expression can be any non-null expression.

Because C# 6 supports only the constant pattern and does not allow the repetition of constant values, case labels define mutually exclusive values, and only one pattern can match the match expression. As a result, the order in which case statements appear is unimportant.

In C# 7, however, because other patterns are supported, case labels need not define mutually exclusive values, and multiple patterns can match the match expression. Because only the statements in the switch section that contains the first matching pattern are executed, the order in which case statements appear is now important. If C# detects a switch section whose case statement or statements are equivalent to or are subsets of previous statements, it generates a compiler error, CS8120, "The switch case has already been handled by a previous case."

switch (C# Reference) MSDN article

与其尝试使用 Switch,我建议您将 if else 语句简化为以下内容:

if (Equals(ImageFormat.Png, imageFormatToConvert) || Equals(ImageFormat.Gif, imageFormatToConvert) || Equals(ImageFormat.Jpeg, imageFormatToConvert)
{
    if (Equals(ImageFormat.Jpeg, imageFormatToConvert))
    {
        ImageCodecInfo[] arrImageCodecInfo = ImageCodecInfo.GetImageEncoders();
        using (EncoderParameters encoderParameters = new EncoderParameters(1))
        {
            encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
            bitmap.Save(targetImageFilePath, arrImageCodecInfo[1], encoderParameters);
        }
    }
    else{
        bitmap.Save(targetImageFilePath, imageFormatToConvert);
    }
}
else
{
    throw new Exception($"Convert to <{imageFormatToConvert.ToString()}> from " +
                        $"<{new ImageFormatConverter().ConvertToString(image.RawFormat)}>" +
                        $" image format is not supported now.");
}

只是 ToString() ImageFormat,然后打开 (string) 结果?

ImageFormat 覆盖 ToString 以产生 "human readable" 结果。参见 MSDN

var desiredImageFormatName = imageFormatToConvert.ToString();

switch (desriredImageFormatName)
{
    case "JPEG": // or whatever ImageFormat.Jpeg.ToString() returns
        ...
}