Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

System.NullReferenceException when cloning a bitmap #21

@AndrewKaplanovsky

Description

@AndrewKaplanovsky

Hi Anders,

Sorry for my annoyance, but when I try to clone a Bitmap, from time to time I get a System.NullReferenceException exception:

at (wrapper unknown) ImagePixelEnumerator.Helpers.Pixels.NonIndexed.PixelDataPArgb8888:PtrToStructure (intptr,object)
at (wrapper managed-to-native) System.Runtime.InteropServices.Marshal:PtrToStructure (intptr,System.Type)
at ImagePixelEnumerator.Helpers.Pixel.ReadRawData (IntPtr imagePointer) [0x00000] in d:\Users\Anders\Documents\Visual Studio 2013\Projects\aforge\Sources\System.Drawing\Helpers\Pixel.cs:272
at ImagePixelEnumerator.Helpers.ImageBuffer.ReadPixel (ImagePixelEnumerator.Helpers.Pixel pixel, System.Byte[] buffer) [0x0001f] in d:\Users\Anders\Documents\Visual Studio 2013\Projects\aforge\Sources\System.Drawing\Helpers\ImageBuffer.cs:211
at ImagePixelEnumerator.Helpers.ImageBuffer+<>c__DisplayClass5.b__4 (ImagePixelEnumerator.Helpers.LineTask lineTask) [0x00075] in d:\Users\Anders\Documents\Visual Studio 2013\Projects\aforge\Sources\System.Drawing\Helpers\ImageBuffer.cs:492
at System.Threading.Tasks.Parallel+c__AnonStorey52[TSource,TLocal].<>m__0 (Int32 i) [0x00000] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/threading/Tasks/Parallel.cs:2318 at System.Threading.Tasks.Parallel+<ForWorker>c__AnonStorey31[TLocal].<>m__1 () [0x000ed] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/threading/Tasks/Parallel.cs:1204

The code is following:

public Bitmap ProcessImage(Bitmap src)
{
if (src == null)
{
return null;
}

 Bitmap image = null;

 try
 {
     image = src.Clone(pixelFormat: PixelFormat.Format24bppRgb); <---- the code fails here
 }
 catch (Exception ex) {
            return null;
 }
...

}

The project is a portable library, I get this error when using it in Droid project. First time when I call this code everything works fine, but then is I call it subsequently, at one step the code just crashes with the exception.

Thank you,
Andrew

Metadata

Metadata

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions