C# Base64 String to JPEG Image

C# Base64 String to JPEG Image

To convert a Base64 string to a JPEG image in C#, you can use the Convert.FromBase64String method to convert the string to a byte array, and then create a MemoryStream from the byte array. Finally, you can use the Image.FromStream method to create a System.Drawing.Image object from the stream.

Here's an example:

string base64String = "Your Base64 string here"; byte[] bytes = Convert.FromBase64String(base64String); using (MemoryStream ms = new MemoryStream(bytes)) { Image image = Image.FromStream(ms); image.Save("output.jpg", ImageFormat.Jpeg); } 

In this example, a string variable base64String contains the Base64-encoded data. The Convert.FromBase64String method is used to convert the string to a byte array. A MemoryStream is created from the byte array, and then the Image.FromStream method is used to create a System.Drawing.Image object from the stream. Finally, the Image.Save method is used to save the image as a JPEG file.

Note that the System.Drawing namespace is not available in .NET Core, so if you are developing a .NET Core application, you may need to use a different approach to work with images.

Examples

  1. "C# Convert Base64 string to JPEG Image"

    • Code:
      string base64String = "YourBase64StringHere"; byte[] imageBytes = Convert.FromBase64String(base64String); using (MemoryStream ms = new MemoryStream(imageBytes)) { Image image = Image.FromStream(ms); image.Save("output.jpg", ImageFormat.Jpeg); } 
    • Description: Converts a Base64 string to a JPEG image and saves it to a file.
  2. "C# Base64 string to byte array conversion"

    • Code:
      string base64String = "YourBase64StringHere"; byte[] imageBytes = Convert.FromBase64String(base64String); 
    • Description: Demonstrates the conversion of a Base64 string to a byte array.
  3. "C# Base64 string to Image object"

    • Code:
      string base64String = "YourBase64StringHere"; byte[] imageBytes = Convert.FromBase64String(base64String); using (MemoryStream ms = new MemoryStream(imageBytes)) { Image image = Image.FromStream(ms); // Use the 'image' object as needed } 
    • Description: Converts a Base64 string to an Image object for further processing.
  4. "C# Display Base64-encoded JPEG image in Windows Forms"

    • Code:
      string base64String = "YourBase64StringHere"; byte[] imageBytes = Convert.FromBase64String(base64String); using (MemoryStream ms = new MemoryStream(imageBytes)) { Image image = Image.FromStream(ms); PictureBox pictureBox = new PictureBox(); pictureBox.Image = image; // Add pictureBox to a Windows Forms form for display } 
    • Description: Displays a Base64-encoded JPEG image in a Windows Forms application using a PictureBox.
  5. "C# Convert Base64 to Bitmap"

    • Code:
      string base64String = "YourBase64StringHere"; byte[] imageBytes = Convert.FromBase64String(base64String); using (MemoryStream ms = new MemoryStream(imageBytes)) { Bitmap bitmap = new Bitmap(ms); // Use the 'bitmap' object as needed } 
    • Description: Converts a Base64 string to a Bitmap object.
  6. "C# Save Base64-encoded JPEG image to a file"

    • Code:
      string base64String = "YourBase64StringHere"; byte[] imageBytes = Convert.FromBase64String(base64String); File.WriteAllBytes("output.jpg", imageBytes); 
    • Description: Saves a Base64-encoded JPEG image to a file using File.WriteAllBytes.
  7. "C# Display Base64-encoded JPEG image in WPF"

    • Code:
      string base64String = "YourBase64StringHere"; byte[] imageBytes = Convert.FromBase64String(base64String); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = new MemoryStream(imageBytes); bitmapImage.EndInit(); // Use 'bitmapImage' in a WPF Image control for display 
    • Description: Displays a Base64-encoded JPEG image in a WPF application using a BitmapImage.
  8. "C# Convert Base64 to ImageSource for WPF"

    • Code:
      string base64String = "YourBase64StringHere"; byte[] imageBytes = Convert.FromBase64String(base64String); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = new MemoryStream(imageBytes); bitmapImage.EndInit(); ImageSource imageSource = bitmapImage as ImageSource; // Use 'imageSource' in a WPF Image control for display 
    • Description: Converts a Base64 string to an ImageSource for use in a WPF Image control.
  9. "C# Convert Base64 to System.Drawing.Image"

    • Code:
      string base64String = "YourBase64StringHere"; byte[] imageBytes = Convert.FromBase64String(base64String); using (MemoryStream ms = new MemoryStream(imageBytes)) { System.Drawing.Image image = System.Drawing.Image.FromStream(ms); // Use the 'image' object as needed } 
    • Description: Converts a Base64 string to a System.Drawing.Image object.
  10. "C# Convert Base64 to OpenCV Mat for image processing"

    • Code:
      string base64String = "YourBase64StringHere"; byte[] imageBytes = Convert.FromBase64String(base64String); Mat mat = Cv2.ImDecode(imageBytes, ImreadModes.Color); // Use the 'mat' object for image processing with OpenCV 
    • Description: Converts a Base64 string to an OpenCV Mat for image processing.

More Tags

findall uigesturerecognizer mysql-error-1170 dayofweek access-token dialog clob android-coordinatorlayout javascript-1.7 pandas

More C# Questions

More Chemical reactions Calculators

More Fitness Calculators

More Financial Calculators

More Math Calculators