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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:31:57+00:00 2026-05-22T17:31:57+00:00

The following code always causes an AccessViolationException in the Fill method when the last

  • 0

The following code always causes an AccessViolationException in the Fill method when the last pixel (x = 255, y = 255) is accessed. However, if I use a size such as 200×200 it works. Same problem with 512 x 512 or 1024 x 1024 or other power-of-two sizes (seems to work fine with 4×4, 8×8 … up to 32 x 32). Any ideas why that is?

var wb = new WriteableBitmap(256, 256, 96, 96, PixelFormats.Bgr24, null);
wb.Fill(256, 256, 3, Colors.Black);

...

public static void Fill(this WriteableBitmap bmp, int width, int height, int bytesPerPixel, Color color)
{
    var rect = new Int32Rect(0, 0, width, height);
    var fillColor = color.ToInt();

    bmp.Lock();

    unsafe
    {
      int pBackBuffer = (int)bmp.BackBuffer;
      int stride = bmp.BackBufferStride;

      for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++)
        {
          int offset = y * stride + x * bytesPerPixel;
          int pBackBufferWithOffset = pBackBuffer + offset;
          *((int*) pBackBufferWithOffset) = fillColor;
        }
    }

    bmp.AddDirtyRect(rect);
    bmp.Unlock();
}

private static int ToInt(this Color color)
{
    int c = color.R << 16; // R
    c    |= color.G << 8;  // G
    c    |= color.B << 0;  // B
    return c;
}
  • 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-22T17:31:58+00:00Added an answer on May 22, 2026 at 5:31 pm

    Two issues.

    (1) Your are assuming that bitsPerPixels is the same as the size of an int when you set the color.

    (2) Why are you multiplying x by 3 when calculting the offset?

    If bitsPerPixels is 32; then (1) is current and (2) should be multiplying x by 4.

    Else if bitsPerPixels is 24 then (1) should be

           *((byte *) pBadkBufferWithOffset + 0) = color.R;
           *((byte *) pBadkBufferWithOffset + 1) = color.G;
           *((byte *) pBadkBufferWithOffset + 2) = color.B;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DataAnnotations does not work with buddy class. The following code always validate true. Why
I am using following PHP code for trigger creation but always get error, please
The following simple program in Java uses the java.util.Random class such that it always
I'm having issues with the following code, where I use a JComboBox to change
I've got the following code: package MyPackage::ResultSet::Case; use base 'DBIx::Class::ResultSet'; sub cases_last_fourteen_days { my
In the following code, I'm trying to send a key-value pair and I always
I have tried to use the following code to passing value back from the
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
Following code iterates through many data-rows, calcs some score per row and then sorts
following code doesn't work with input: 2 7 add Elly 0888424242 add Elly 0883666666

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.