chugathon

Author Topic: .webp support  (Read 62 times)

Offline spuuunit

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
.webp support
« on: October 09, 2020, 06: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.

Offline Richard Moss

  • Cyotek Team
  • Administrator
  • Sr. Member
  • *****
  • Posts: 406
  • Karma: +22/-0
    • cyotek.com
Re: .webp support
« Reply #1 on: October 11, 2020, 04: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 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

Offline spuuunit

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
Re: .webp support
« Reply #2 on: October 15, 2020, 08: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.


Code: [Select]
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?

Offline Richard Moss

  • Cyotek Team
  • Administrator
  • Sr. Member
  • *****
  • Posts: 406
  • Karma: +22/-0
    • cyotek.com
Re: .webp support
« Reply #3 on: October 16, 2020, 05: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 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

Code: [Select]
    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

Offline spuuunit

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
Re: .webp support
« Reply #4 on: October 16, 2020, 08:10:13 PM »
Oh I see... I'll try that out. Thanks!  :)