Skip to content

[Bug Suspected] ImageFormat's "ToString" comparation error #20332

@ghost

Description

@ghost

Let's get started with a very simple demo:

  1. Create a demo console application and referring net 4.6 framework, with the "System.Drawing" namespace added in.

  2. Download or create a file of jpg.

  3. Now let's import the file directly into the Image and do this following by:

Image img = Image.FromFile("c:\\test.jpg"); Console.WriteLine(img.RawFormat.Equals(ImageFormat.Jpeg));

Now the answer is "True", and the ImageFormat's Guid for "JPEG" is "{b96b3cae-0728-11d3-9d7b-0000f81ef32e}". The same answer is also for "Equals".

HOWEVER in "ToString" method, the expected result should be "Jpeg" as the output. But I didn't see that, and instead the result is:

[ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]

Now let's deeply go into the code (to here), the original source code is from: https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Advanced/ImageFormat.cs,b96b3cae-0728-11d3-9d7b-0000f81ef32e,references

public override string ToString()
{
if (this == memoryBMP) return "MemoryBMP";
if (this == bmp) return "Bmp";
if (this == emf) return "Emf";
if (this == wmf) return "Wmf";
if (this == gif) return "Gif";
if (this == jpeg) return "Jpeg";
if (this == png) return "Png";
if (this == tiff) return "Tiff";
if (this == exif) return "Exif";
if (this == icon) return "Icon";
return "[ImageFormat: " + guid + "]";
}

Why do we say "this == another type of ImageFormat" without overridding the operator "=="? And from this demo can we make sure that this is a bug and should be fixed?
And what's more——Why not use "if……else if……else……" instead of multiple "if" here?
PS:I didn't find the ImageFormat.cs yet, though I did a full research.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions