Cyotek Forums

Source Code => Source Code => Topic started by: spuuunit on October 09, 2020, 07:12:32 PM

Title: .webp support
Post by: spuuunit on October 09, 2020, 07:12:32 PM
Have you considered making .webp support? It would be really neat to be able to view .webp pictures, since they're becoming more and more common on the internet.
Title: Re: .webp support
Post by: Richard Moss on October 11, 2020, 05:54:12 PM
Hello,

The ImageBox is designed to work with .NET System.Drawing.Image instances. Therefore, if you can load an image using webp format into this, ImageBox will display it.

I tend to use the FreeImage (https://freeimage.sourceforge.io/) for loading webp (and various other formats) images into .NET. I'll see if I can add a demo of this to the ImageBox project when I'm next updating it.

Regards;
Richard Moss
Title: Re: .webp support
Post by: spuuunit on October 15, 2020, 09:27:01 PM
Oh ok... I made a C# project where I included ImageBox (nugget), but webp pictures gives an exception for me. System.OutOfMemoryException.


imgBox.Image = Image.FromFile(@"D:\Bilder\Trams\barren.jpg");  // works
imgBox.Image = Image.FromFile(@"D:\Bilder\Trams\webp.webp");  // gives exception


using (FileStream file_stream = new FileStream(@"D:\Bilder\Trams\barren.jpg", FileMode.Open))  // works
{
MemoryStream memory_stream = new MemoryStream();
file_stream.CopyTo(memory_stream);
imgBox.Image = Image.FromStream(memory_stream);
}
using (FileStream file_stream = new FileStream(@"D:\Bilder\Trams\webp.webp", FileMode.Open))  // gives exception
{
MemoryStream memory_stream = new MemoryStream();
file_stream.CopyTo(memory_stream);
imgBox.Image = Image.FromStream(memory_stream);
}


Any idea why this is?
Title: Re: .webp support
Post by: Richard Moss on October 16, 2020, 06:44:32 AM
Hello,

.NET doesn't have decoders for the webp format, so you'd either need to write your own, or use a 3rd parties. As I noted in my earlier reply, I use the FreeImage (https://freeimage.sourceforge.io/) library. This is a C DLL (in 32 or 64bit varieties and you need to distribute the correct one for your application) and includes a .NET wrapper that you can either embed in your application or use as a normal reference.

The following snippet shows how to use the FreeImage wrapper to load any image format it supports, and convert to a .NET bitmap

    public static Bitmap FromFile(string fileName)
    {
      FIBITMAP imageData;
      Bitmap result;

      if (string.IsNullOrEmpty(fileName))
      {
        throw new ArgumentNullException(nameof(fileName));
      }

      if (!File.Exists(fileName))
      {
        throw new FileNotFoundException(string.Format("Cannot find file '{0}'.", fileName), fileName);
      }

      imageData = new FIBITMAP();

      try
      {
        imageData = FreeImage.LoadEx(fileName);

        if (imageData.IsNull)
        {
          throw new ArgumentException("Failed to load image.", nameof(fileName));
        }

        result = FreeImage.GetBitmap(imageData);
      }
      finally
      {
        FI.Unload(imageData);
      }

      return result;
    }


(One nice thing about this approach, unlike Image.FromFile, it doesn't lock the underlying file so no need to jump hoops)

FreeImage hasn't been updated for a few years now and there may be better alternatives available so it may be worth doing a search for "c# webp" and see what's out there.

Regards;
Richard Moss
Title: Re: .webp support
Post by: spuuunit on October 16, 2020, 09:10:13 PM
Oh I see... I'll try that out. Thanks!  :)