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

  • SEARCH
  • Home
  • 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 7778703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:27:51+00:00 2026-06-01T18:27:51+00:00

I have the following two functions to convert bytes to image and display on

  • 0

I have the following two functions to convert bytes to image and display on Image in WPF

 private JpegBitmapDecoder ConvertBytestoImageStream(byte[] imageData)
        {
            Stream imageStreamSource = new MemoryStream(imageData);            

            JpegBitmapDecoder decoder = new JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];

            return decoder;
        }

The above code does not work at all. I always get the exception that “No imaging component found” Image is not displayed.

private MemoryStream ConvertBytestoImageStream(int CameraId, byte[] ImageData, int imgWidth, int imgHeight, DateTime detectTime)
    {  
        GCHandle gch = GCHandle.Alloc(ImageData, GCHandleType.Pinned);
        int stride = 4 * ((24 * imgWidth + 31) / 32);
        Bitmap bmp = new Bitmap(imgWidth, imgHeight, stride, PixelFormat.Format24bppRgb, gch.AddrOfPinnedObject());
        MemoryStream ms = new MemoryStream();
        bmp.Save(ms, ImageFormat.Jpeg);
        gch.Free();

        return ms;
    }

This function works, but is very slow. I wish to optimize my code.

  • 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-01T18:27:53+00:00Added an answer on June 1, 2026 at 6:27 pm

    Your ConvertBytestoImageStream works fine for me if i pass it a JPEG buffer. There are however a few things that could be improved. Depending on whether you really want to return a decoder or a bitmap, the method could be written this way:

    private BitmapDecoder ConvertBytesToDecoder(byte[] buffer)
    {
        using (MemoryStream stream = new MemoryStream(buffer))
        {
            return BitmapDecoder.Create(stream,
                BitmapCreateOptions.PreservePixelFormat,
                BitmapCacheOption.OnLoad); // enables closing the stream immediately
        }
    }
    

    or this way:

    private ImageSource ConvertBytesToImage(byte[] buffer)
    {
        using (MemoryStream stream = new MemoryStream(buffer))
        {
            BitmapDecoder decoder = BitmapDecoder.Create(stream,
                BitmapCreateOptions.PreservePixelFormat,
                BitmapCacheOption.OnLoad); // enables closing the stream immediately
            return decoder.Frames[0];
        }
    }
    

    Note that instead of using JpegBitmapDecoder this code utilizes a static factory method of the abstract base class BitmapDecoder which automatically selects the proper decoder for the provided data stream. Hence this code can be used for all image formats supported by WPF.

    Note also that the Stream object is used inside a using block which takes care of disposing it when it is no longer needed. BitmapCacheOption.OnLoad ensures that the whole stream is loaded into the decoder and can be closed afterwards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
I trying to display image in picture box. The application have two part. First
I have the following two functions: $(.content).click( function() { var id = this.id; $.get(InfoRetrieve,
I have the following two functions which take two dictionaries and recursively add their
I have the following two Python functions: @classmethod def serialize_dict(cls, d): values = []
I have the following two class functions which I'm attempting to merge into one
I have the following two functions that work great: $(function(){ $('.trash_can a.delete').live('click', function(event) {
According to MSDN , a hash function must have the following properties: If two
I have following two arrays. I want the difference between these two arrays. That
How to wrap one div around another? I have following two div ids: #course

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.