Can not create Bitmap from byte?

mc 5,186 Reputation points
2023-10-12T11:18:22.35+00:00

I have byte arrary of bitmap which is created by c++ BITMAPFILEHEADER and BitmapInfo

in .net I want to create it to bitmap or image.

in winui3.0 I tried

CanvasBitmap.CreateFromBytes

but I do not know what format it is.

in wpf I tried

WriteableBitmap 

but can not too.

I can use

System.IO.WriteAllBytes("d:\\1.jpeg",my_bytes) 

to write the byte to jpeg in my disk.

and If I read it by BitmapImage in wpf the format is Bgr32

BitmapSource.Create(176, 144, 0, 0, PixelFormats.Bgr32, null,data 176*3);

can not create it too.Value does not fall within the expected

how to do it ?

and I will change the image very frequently so I can not create a new one but change its pixels.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,853 questions
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
866 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 88,376 Reputation points
    2023-10-12T11:49:35.3533333+00:00

    It works with a WriteableBitmap

    I used it for example in WinUI 3 in this test : CMediaEngine.cs

    (function GetCaptureWriteableBitmap where I get a byte array (pPixels) from a Win32 hBitmap

    then WriteableBitmap.PixelBuffer.AsStream().WriteAsync to write the byte array into the WriteableBitmap)

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,651 Reputation points Microsoft External Staff
    2023-10-12T14:46:47.39+00:00

    Hi,@mc. Welcome Microsoft Q&A. Here are some solutions that may work for you in WPF.

    You could load the byte array into a MemoryStream and then create a BitmapImage from that MemoryStream. This approach should work with various common image formats.

    Load the byte array into a MemoryStream.

    public BitmapImage ToImage(byte[] array)
    {
        using (var ms = new System.IO.MemoryStream(array))
        {
            var image = new BitmapImage();
            image.BeginInit();
            image.CacheOption = BitmapCacheOption.OnLoad; // here
            image.StreamSource = ms;
            image.EndInit();
            return image;
        }
    }
    
    

    From the Remarks section in BitmapImage.StreamSource:

    Set the CacheOption property to BitmapCacheOption.OnLoad if you wish to close the stream after the BitmapImage is created.

    Besides that, you can also use built-in type conversion to convert from type byte[] to type ImageSource (or the derived BitmapSource):

    
    var bitmap = (BitmapSource)new ImageSourceConverter().ConvertFrom(array);
    
    

    Method that uses the ImageConverter object in .Net Framework to convert a byte array, presumably containing a JPEG or PNG file image, into a Bitmap object, which can also be used as an Image object.

    
      /// <param name="byteArray">byte array containing JPEG or PNG file image or similar</param>
    
      public static Bitmap GetImageFromByteArray(byte[] byteArray)
      {
         Bitmap bm = (Bitmap)_imageConverter.ConvertFrom(byteArray);
    
         if (bm != null && (bm.HorizontalResolution != (int)bm.HorizontalResolution ||
                            bm.VerticalResolution != (int)bm.VerticalResolution))
         {
            // Correct a strange glitch that has been observed in the test program when converting 
            //  from a PNG file image created by CopyImageToByteArray() - the dpi value "drifts" 
            //  slightly away from the nominal integer value
            bm.SetResolution((int)(bm.HorizontalResolution + 0.5f), 
                             (int)(bm.VerticalResolution + 0.5f));
         }
    
         return bm;
      }
    

    Or you can refer to the solution here to see if it is helpful to you.

    If you still have questions, please feel free to let me know.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.