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 204947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:30:46+00:00 2026-05-11T17:30:46+00:00

I am trying to dynamically create a Bitmap Image using byte array using following

  • 0

I am trying to dynamically create a Bitmap Image using byte array using following code

Bitmap GetImage()
{
    IntPtr  ip = Marshal.AllocCoTaskMem(imagesize);

    //some code to fill ip

    Image img =  new Bitmap(
                    w,
                    h,
                    -stride,
                    PixelFormat.Format24bppRgb,
                    (IntPtr)(ip.ToInt32() + imagesize - stride)
                    );

    Marshal.FreeCoTaskMem( ip); // Comment this line to work

    return img;
}

void SaveImage()
{
    Image img =  GetImage();
    img.save("test.bmp"); // This call fails
    img.Dispose();
}

Now my problem is I want to return a Bitmap but at the same time I don’t want to keep the ip pointer as that memory allocated leaks. It is never freed.
How can I return a Image created with bytes array and without bothering the caller of the function GetImage() to free up the memory.

  • 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-05-11T17:30:46+00:00Added an answer on May 11, 2026 at 5:30 pm

    The simplest way may be to create the bitmap (although in similar code I’ve used a normal byte[] and then a fixed block, rather than directly using AllocCoTaskMem), and then create a second Bitmap of the same size. Call Graphics.FromImage on the second bitmap, then Graphics.DrawImage using the first bitmap to effectively copy the contents of the first bitmap onto the second. You can then call Dispose on the first bitmap, release the memory, and use the second bitmap which is basically a copy.

    There may well be a more efficient way to do it if you’re more proficient with image stuff, but if you’re just looking for a way to get it to work, it’s a starting point 🙂

    EDIT: zachrrs’s comment does indeed make things easier using the Bitmap(Image) constructor:

    using (Bitmap original = new Bitmap(...))
    {
        Bitmap copy = new Bitmap(originalImage);
        // Then return "copy" from your method, and you can free the
        // memory
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use Virtualenv. There is more information here: Working with virtualenv.… May 11, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer You always need to install on your build server the… May 11, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer Django doesn't really process dynamic including like PHP or other… May 11, 2026 at 11:43 pm

Related Questions

I am trying to render some XAML at runtime. My XAML is dynamically constructed
I am trying to send my dynamically created silverlight 2 page/image to a an
I'm trying to create an object in a function, but I am running into
I would like to be able to dynamically retrieve the current executing module or

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.