Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9122109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:01:06+00:00 2026-06-17T06:01:06+00:00

I have a problem. I want to convert BitmapImage into byte[] array and back.

  • 0

I have a problem. I want to convert BitmapImage into byte[] array and back.

I wrote these methods:

public static byte[] ToByteArray(this BitmapImage bitmapImage)
{
    byte[] bytes;
    using (MemoryStream ms = new MemoryStream())
    {
        bitmapImage.BeginInit();
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.StreamSource.CopyTo(ms);
        bitmapImage.EndInit();
        bytes = ms.ToArray();
    }
    return bytes;
}

public static BitmapImage ToBitmapImage(this byte[] bytes, int width, int height)
{
    BitmapImage bitmapImage = new BitmapImage();
    using (MemoryStream ms = new MemoryStream(bytes))
    {
        ms.Position = 0;
        bitmapImage.BeginInit();
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.StreamSource = ms;
        bitmapImage.EndInit(); // HERE'S AN EXCEPTION!!!
    }
    return bitmapImage;
}

First one works fine, but when I try to convert from byte[] into BitmapImage I got a NotSupportedException… Why? How to correct the code of the 2nd method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:01:08+00:00Added an answer on June 17, 2026 at 6:01 am

    There are two problems with your ToByteArray method.

    First it calls BeginInit and EndInit on an already initialized BitmapImage instance. This is not allowed, see the Exceptions list in BeginInit.

    Second, the method could not be called on a BitmapImage that was created from an Uri instead of a Stream. Then the StreamSource property would be null.

    I suggest to implement the method like shown below. This implementation would work for any BitmapSource, not only BitmapImages. And you are able to control the image format by selecting an appropriate BitmapEncoder, e.g. JpegBitmapEncoder instead of PngBitmapEncoder.

    public static byte[] ToByteArray(this BitmapSource bitmap)
    {
        var encoder = new PngBitmapEncoder(); // or any other encoder
        encoder.Frames.Add(BitmapFrame.Create(bitmap));
    
        using (var ms = new MemoryStream())
        {
            encoder.Save(ms);
            return ms.ToArray();
        }
    }
    

    An image buffer returned by this ToByteArray method can always be converted back to a BitmapImage by your ToBitmapImage method.

    And please note that the width and height arguments of your ToBitmapImage method are currently unused.


    UPDATE

    An alternative implementation of the decoding method could look like shown below, although it does not return a BitmapImage but only an instance of the base class BitmapSource. You may however change the return type to BitmapFrame.

    public static BitmapSource ToBitmapImage(this byte[] bytes)
    {
        using (var stream = new MemoryStream(bytes))
        {
            var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
            return decoder.Frames[0];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have smalll problem i want to convert unicode into Multi byte is there
I have this problem in split I want to convert the numeric into formatcurrency
My problem is, when I want to convert (result[i].JobOrder) to int.I have a string
I have a problem when I want to sending data using byte format in
I have the following problem. I have an array of bytes that I want
I want to convert a SOAPMessage to a byte Array so i can encrypt
Simple problem. I have an SQL Server database (MyData.mdf) and I want to convert
I have following problem: I want to convert some Binary Strings to an integer:
I have .haml files and want to convert them automatically into .html files and
I have a problem and need a help from you! I want to convert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.