Main Menu

.webp support

Started by spuuunit, October 09, 2020, 07:12:32 PM

Previous topic - Next topic

spuuunit

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.

Richard Moss

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 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
Read "Before You Post" before posting (https://forums.cyotek.com/cyotek-webcopy/before-you-post/). Do not send me private messages. Do not expect instant replies.

All responses are hand crafted. No AI involved. Possibly no I either.

spuuunit

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?

Richard Moss

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 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
Read "Before You Post" before posting (https://forums.cyotek.com/cyotek-webcopy/before-you-post/). Do not send me private messages. Do not expect instant replies.

All responses are hand crafted. No AI involved. Possibly no I either.

spuuunit

Oh I see... I'll try that out. Thanks!  :)