Download a webp image and load to PictureBox.Image

kiwis

Member
Joined
Jan 26, 2025
Messages
7
Programming Experience
Beginner
As the heading says, i'm trying to download a webp image then load it into my picturebox.

This is the code, I get the image but it's now showing

What's wrong?

C#:
HttpClient client = new HttpClient();
string _url = "https://www.mydomain.com/images/" + profile.imageSrc;
_ = getImage(client, _url);


C#:
        private async Task getImage(HttpClient client, string _url)
        {
            byte[] data = await client.GetByteArrayAsync(_url);
            File.WriteAllBytes("image.jpg", data);
            PictureBox1.Image = Image.FromFile(@"image.jpg");
        }
 
Read the documentation:
Managed GDI+ has built-in encoders and decoders that support the following file types:
  • BMP
  • GIF
  • JPEG
  • PNG
  • TIFF

Downloading a .webp file but saving it as a .jpeg doesn't suddenly convert a .webp file into a .jpeg file.
 
Of course, how can I convert it?
Of course, how can I convert it?

I don't know the answer to that question but I bet I could find it with a web search. If I could find that information with a web search, so can you. I'm not trying to discourage you from using this site - far from it - but you should always look for yourself first and then ask others if you can't find what you need or can't understand what you find. Converting an image file from a non-standard format to a standard Windows-supported format is not a C# issue, so it's not really something to ask in a C# forum. If you find information on how to do it and need help implementing it in C# then that's a good question for this site.
 
I just did the appropriate search and, not surprisingly, found that there's no obvious way to do this natively in .NET. I see lots of online converters and some Python script but no NuGet package or the like. Maybe you can find one if you look more thoroughly but I suspect not. That means that you probably have three options:
  1. Use Python.
  2. Convert the files yourself using an online tool and allow the app access to the output.
  3. Use a WebView2 control to display the WEBP file natively.
Option 1 would definitely be possible, but I have no experience in doing it. That's something that, again, you should look up for yourself and then ask for help if and only if you need it. It might be advantageous to use IronPython, which is a .NET-friendly version of Python, or it might be simpler to just execute an external script file.

Option 2 would be fairly simple but may not be practical for what you're trying to achieve.

Option 3 may actually be the simplest option, as you can probably just add a WebView2 control to your form and then navigate to the online image location directly or a local file path.
 
See post #5.
 
Back
Top Bottom