validateImageData parameter and Image.FromStream()

前端 未结 4 1534
忘掉有多难
忘掉有多难 2021-02-13 07:12

I\'m concerned about the third parameter in this overload, validateImageData. The documentation doesn\'t explain much about it, it only states that it causes the image data to b

4条回答
  •  遇见更好的自我
    2021-02-13 07:38

    From Reflector, we see:

    if (validateImageData)
    {
        num = SafeNativeMethods.Gdip.GdipImageForceValidation(new HandleRef(null, zero));
        if (num != 0)
        {
            SafeNativeMethods.Gdip.GdipDisposeImage(new HandleRef(null, zero));
            throw SafeNativeMethods.Gdip.StatusException(num);
        }
    }
    

    So we see that GdipImageForceValidation is called (recall, System.Drawing is just a wrapper over GDI+). The documentation for this function isn't very good:

    This function forces validation of the image.

    Not very useful. However, the point is made - the image file is interrogated to ensure it is safe to load. This may cause the whole image to be loaded into memory.

    If you are accepting inputs from users, I certainly would set this flag to true - you never know what kind of files (malformed or otherwise) users will upload. Better safe than sorry. This is why the default is true.

    Note also that GDI+ is not recommended for server environments. You're better off using System.Windows.Media.Imaging.

提交回复
热议问题