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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:13:43+00:00 2026-05-24T19:13:43+00:00

I have the following code: BitmapSource bitmap = _bitmapSource; if (_bitmapSource.Format != PixelFormats.Bgra32) bitmap

  • 0

I have the following code:

BitmapSource bitmap = _bitmapSource;

if (_bitmapSource.Format != PixelFormats.Bgra32)
    bitmap = new FormatConvertedBitmap(_bitmapSource, PixelFormats.Bgra32, null, 0);

int bytesPerPixel = (bitmap.Format.BitsPerPixel + 7) / 8;
int pixelWidth = bitmap.PixelWidth;
int pixelHeight = bitmap.PixelHeight;
int stride = bytesPerPixel * pixelWidth;
int pixelCount = pixelWidth * pixelHeight;
var pixelBytes = new byte[pixelCount * bytesPerPixel];

bitmap.CopyPixels(pixelBytes, stride, 0);

...

}

A NUnit test exercises this code which throws when it reaches bitmap.CopyPixels:

System.IO.FileFormatException : The image decoder cannot decode the image. The image might be corrupted.
    ----> System.Runtime.InteropServices.COMException : Exception from HRESULT: 0x88982F60

at System.Windows.Media.Imaging.BitmapSource.CriticalCopyPixels(Int32Rect sourceRect, IntPtr buffer, Int32 bufferSize, Int32 stride)
at System.Windows.Media.Imaging.BitmapSource.CriticalCopyPixels(Int32Rect sourceRect, Array pixels, Int32 stride, Int32 offset)
at System.Windows.Media.Imaging.BitmapSource.CopyPixels(Int32Rect sourceRect, Array pixels, Int32 stride, Int32 offset)
at System.Windows.Media.Imaging.BitmapSource.CopyPixels(Array pixels, Int32 stride, Int32 offset)

however the image is not corrupted (other tests use the same file without issue) and strangely if I set a breakpoint at bitmap.CopyPixels and break in the debugger then continue, the exception is not thrown.

Can anyone shed any light on what might be causing the error for me?

  • 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-24T19:13:45+00:00Added an answer on May 24, 2026 at 7:13 pm

    I have solved this, it was quite simple.

    _bitmapSource was created earlier using a FileStream like so:

    using(var f = new FileStream(imagePath,FileMode.Open,FileAccess.Read){
        BitmapSource bitmap = BitmapFrame.Create(f);
    }
    CallToCodeInQuestion(bitmap);
    

    In the docs for BitmapFrame.Create it says

    The bitmapStream can be closed after the frame is created only when
    the OnLoad cache option is used. The default OnDemand cache option
    retains the stream until the frame is needed. Use the Create(Stream,
    BitmapCreateOptions, BitmapCacheOption) method to specify create and
    cache options.

    So I needed to do this:

    using(var f = new FileStream(imagePath, FileMode.Open, FileAccess.Read){
        BitmapSource bitmap = BitmapFrame.Create(
            f,
            BitmapCreateOptions.None,
            BitmapCacheOptions.OnLoad);
    }//FileStream has been closed.
    CallToCodeInQuestion(bitmap);
    

    My NUnit test now passes.

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

Sidebar

Related Questions

I have following code snippet in c# List<int> list = new List<int>() { 1,
I have following code class User attr_accessor :name end u = User.new u.name =
There is something I am missing. Say I have the following code: private Bitmap
I have following code: $telnums = array(10, 20, 30); $obj = new StdClass(); $obj->telnums
I have following code private static ObservableCollection<myColor> myItems = new ObservableCollection<myColor>(); myItems.Add(new myColor(red)); When
I have following code in the form_Load function System.Windows.Forms.Timer newtimer = new Timer(); newtimer.Tick
I have following code to serialize my data into a file: out = new
I have following code: [TestMethod] public void StartWorkInFirstThread() { if (SynchronizationContext.Current == null) SynchronizationContext.SetSynchronizationContext(
I have following code class Test { public: int &ref; int a; Test(int &x)
I have following code: typedef int (*t_Function) (int x); t_Function Functions[MAX_FUNCTIONS]; int f(int x)

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.