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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:54:33+00:00 2026-05-21T10:54:33+00:00

Im currently trying to use writeablebitmap to take a IntPtr of a scan of

  • 0

Im currently trying to use writeablebitmap to take a IntPtr of a scan of images and turn each one into a Bitmap. Im wanting to use writeablebitmap because im having an issue with standard gdi
GDI+ System.Drawing.Bitmap gives error Parameter is not valid intermittently

There is a method on a WriteableBitmap that called WritePixels
http://msdn.microsoft.com/en-us/library/aa346817.aspx

Im not sure what I set for the buffer and the stride every example I find it shows the stride as 0 although that throws an error. When I set the stride to 5 the image appear black. I know this may not be the most efficient code but any help would be appreciated.

//create bitmap header
bmi = new BITMAPINFOHEADER();

//create initial rectangle
Int32Rect rect = new Int32Rect(0, 0, 0, 0);

//create duplicate intptr to use while in global lock
dibhand = dibhandp;
bmpptr = GlobalLock(dibhand);

//get the pixel sizes
pixptr = GetPixelInfo(bmpptr);

//create writeable bitmap
var wbitm = new WriteableBitmap(bmprect.Width, bmprect.Height, 96.0, 96.0, System.Windows.Media.PixelFormats.Bgr32, null);

//draw the image
wbitm.WritePixels(rect, dibhandp, 10, 0);

//convert the writeable bitmap to bitmap
var stream = new MemoryStream();

var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(wbitm));
encoder.Save(stream);

byte[] buffer = stream.GetBuffer();
var bitmap = new System.Drawing.Bitmap(new MemoryStream(buffer));

GlobalUnlock(dibhand);
GlobalFree(dibhand);
GlobalFree(dibhandp);
GlobalFree(bmpptr);
dibhand = IntPtr.Zero;

return bitmap;
  • 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-21T10:54:34+00:00Added an answer on May 21, 2026 at 10:54 am

    An efficient way to work on Bitmaps in C# is to pass temporarily in unsafe mode (I know I don’t answer the question exactly but I think the OP did not manage to use Bitmap, so this could be a solution anyway). You just have to lock bits and you’re done:

    unsafe private void GaussianFilter()
    {
        // Working images
        using (Bitmap newImage = new Bitmap(width, height))
        {
            // Lock bits for performance reason
            BitmapData newImageData = newImage.LockBits(new Rectangle(0, 0, newImage.Width,
                newImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    
            byte* pointer = (byte*)newImageData.Scan0;
            int offset = newImageData.Stride - newImageData.Width * 4;
    
            // Compute gaussian filter on temp image
            for (int j = 0; j < InputData.Height - 1; ++j)
            {
                for (int 0 = 1; i < InputData.Width - 1; ++i)
                {
                    // You browse 4 bytes per 4 bytes
                    // The 4 bytes are: B G R A
                    byte blue = pointer[0];
                    byte green = pointer[1];
                    byte red = pointer[2];
                    byte alpha = pointer[3];
    
                    // Your business here by setting pointer[i] = ...
                    // If you don't use alpha don't forget to set it to 255 else your whole image will be black !!
    
                    // Go to next pixels
                    pointer += 4;
                }
                // Go to next line: do not forget pixel at last and first column
                pointer += offset;
            }
    
    
            // Unlock image
            newImage.UnlockBits(newImageData);
            newImage.Save("D:\temp\OCR_gray_gaussian.tif");
        }
    }
    

    This is really much more efficient than SetPixel(i, j), you just have to be careful about pointer limits (and not forget to unlock data when you’re done).

    Now to answer your question about stride: the stride is the length in bytes of a line, it is a multiple of 4. In my exemple I use the format Format32bppArgb which uses 4 bytes per pixel (R, G, B and alpha), so newImageData.Stride and newImageData.Width * 4 are always the same. I use the offset in my loops only to show where it would be necessary.

    But if you use another format, for instance Format24bppRgb which uses 3 bytes per pixel (R, G and B only), then there may be an offset between stride and width. For an image 10 * 10 pixels in this format, you will have a stride of 10 * 3 = 30, + 2 to reach nearest multiple of 4, i.e. 32.

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

Sidebar

Related Questions

I'm currently trying to use NHibernate.Search, but i need to get score for each
I'm currently trying to use Howard Hinnant's unique_ptr implementation , and am running into
I am currently trying to use NAnt and CruiseControl.NET to manage various aspects of
I currently use VS 2008 Professional, but I'm trying to get Team System Developer
I'm currently trying to get into the Java EE development with the Spring framework.
I'm currently trying to use a certain SDK that has me loading functions off
I'm currently trying to use the YouTube API as part of a jQuery plugin
I am currently trying to use the CloudDrive (Powershell) sample that comes with the
I am currently trying to use a web service I developed within an Integration
I'm currently trying to use regular expressions in C#: Regex reg_gameinfo = new Regex(@PokerStars

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.