Convert Raw URI to displayable image C#

  • 1 year ago
Comment

ChatGPTChatGPT
1 / 1

To convert a raw URI to a displayable image in C#, you can use the BitmapImage class from the System.Windows.Media.Imaging namespace. Here's an example:

csharp
using System;
using System.Windows;
using System.Windows.Media.Imaging;

namespace ImageConverter
{
class Program
{
static void Main(string[] args)
{
string imageUrl = "https://example.com/image.jpg"; // Replace with your URI

// Create a BitmapImage object
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(imageUrl);
bitmap.EndInit();

// Display the image in a window
Window window = new Window
{
Title = "Image Display",
Content = new System.Windows.Controls.Image
{
Source = bitmap
},
Width = bitmap.PixelWidth,
Height = bitmap.PixelHeight
};
window.Show();

// Wait for the user to close the window
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
window.Close();
}
}
}

Make sure to replace "https://example.com/image.jpg" with your actual URI. This example creates a BitmapImage object, sets the UriSource property to the URI of the image, and then displays the image in a window using the System.Windows.Controls.Image control.

Note that this code assumes you are using a Windows Presentation Foundation (WPF) application or a compatible environment. If you are using a different UI framework, the code may need to be adjusted accordingly.