【问题标题】:Difference between Bitmap.FromFile(path) and new Bitmap(path)Bitmap.FromFile(path) 和 new Bitmap(path) 之间的区别
【发布时间】:2015-11-19 08:28:24
【问题描述】:

我想知道这两者的区别:

Bitmap bitmap1 = new Bitmap("C:\\test.bmp");
Bitmap bitmap2 = (Bitmap) Bitmap.FromFile("C:\\test.bmp");

一种选择比另一种更好吗? Bitmap.FromFile(path) 是否将任何其他数据填充到位图图像中,还是只是 new Bitmap(path) 的委托?

【问题讨论】:

  • FWIW 你从派生类调用静态方法,它实际上是Image.FromFile。并不是说它改变了问题。

标签: c# image graphics bitmap


【解决方案1】:

“FromFile”方法来自抽象基类Image,它返回一个Image 对象。而Bitmap 类继承Image 类,Bitmap 构造函数允许您直接初始化Bitmap 对象。

在您的第二行代码中,您要做的是调用FromFile() 并获取一个Image 对象,然后将其类型转换为Bitmap。当位图constructor 可以为您执行此操作时,手动执行此操作并不是一个很好的理由。

【讨论】:

    【解决方案2】:

    很难说 - 在内部,这两种方法都非常接近,除了 Image.FromFile() 将检查文件是否存在,如果不存在则抛出 FileNotFoundException

    主要区别在于Bitmap.ctor()在内部调用GdipCreateBitmapFromFile,而Image.FromFile()调用GdipLoadImageFromFile

    这些 Gdip 方法导致两篇 MSDN 文章 (Bitmap.ctor() & Image.FromFile()) 彼此非常接近,但支持的文件格式似乎不同:

    Bitmap: BMP, GIF, JPEG, PNG, TIFF, Exif, WMF, and EMF
    Image:  BMP, GIF, JPEG, PNG, TIFF, and EMF.
    

    无论如何,如果你知道你会有位图,我更喜欢new Bitmap("C:\\test.bmp") 只是为了摆脱之后投射图像的需要。

    【讨论】:

      【解决方案3】:

      这两种方法都通过path 参数获取图像的句柄。 Image.FromFile 将返回超类 Image,而前者将仅返回 Bitmap,因此您可以避免强制转换。

      在内部,他们几乎做同样的事情:

      public static Image FromFile(String filename,
                                           bool useEmbeddedColorManagement)
      {
      
          if (!File.Exists(filename)) 
          {
              IntSecurity.DemandReadFileIO(filename);
              throw new FileNotFoundException(filename);
          }
      
          filename = Path.GetFullPath(filename);
      
          IntPtr image = IntPtr.Zero;
          int status;
      
          if (useEmbeddedColorManagement) 
          {
              status = SafeNativeMethods.Gdip.GdipLoadImageFromFileICM(filename, out image);
          }
          else 
          {
              status = SafeNativeMethods.Gdip.GdipLoadImageFromFile(filename, out image);
          }
      
          if (status != SafeNativeMethods.Gdip.Ok)
              throw SafeNativeMethods.Gdip.StatusException(status);
      
          status = SafeNativeMethods.Gdip.GdipImageForceValidation(new HandleRef(null, image));
      
          if (status != SafeNativeMethods.Gdip.Ok)
          {
              SafeNativeMethods.Gdip.GdipDisposeImage(new HandleRef(null, image));
              throw SafeNativeMethods.Gdip.StatusException(status);
          }
      
          Image img = CreateImageObject(image);
          EnsureSave(img, filename, null);
      
          return img;
      }
      

      还有:

      public Bitmap(String filename) 
      {
          IntSecurity.DemandReadFileIO(filename);
          filename = Path.GetFullPath(filename);
      
          IntPtr bitmap = IntPtr.Zero;
      
          int status = SafeNativeMethods.Gdip.GdipCreateBitmapFromFile(filename, out bitmap);
      
          if (status != SafeNativeMethods.Gdip.Ok)
              throw SafeNativeMethods.Gdip.StatusException(status);
      
          status = SafeNativeMethods.Gdip.GdipImageForceValidation(new HandleRef(null, bitmap));
      
          if (status != SafeNativeMethods.Gdip.Ok) 
          {
              SafeNativeMethods.Gdip.GdipDisposeImage(new HandleRef(null, bitmap));
              throw SafeNativeMethods.Gdip.StatusException(status);
          }
      
          SetNativeImage(bitmap);
      
          EnsureSave(this, filename, null);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-26
        • 1970-01-01
        • 1970-01-01
        • 2017-04-18
        • 2012-08-21
        • 1970-01-01
        相关资源
        最近更新 更多