locked
Exception in WritableBitmap RRS feed

  • General discussion

  • Sometimes (not always) I've got the followed error:

    System.NullReferenceException: Object reference not set to an instance of an object.
    at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
    at MS.Internal.XcpImports.WriteableBitmap_Invalidate(WriteableBitmap wb)
    at System.Windows.Media.Imaging.WriteableBitmap.Invalidate()

    Is the anyone see that exception? What's the reason of that error?

    Sunday, August 30, 2009 12:10 PM

All replies

  • You get this exception if at the time than you calling wb.Invalidate() wb is null. Take a look at your code and make sure you created your WritableBitmap before using it.

    Sunday, August 30, 2009 4:55 PM
  •  If you look at my stack trace, you'll see that ws can't be null. Exception throws in XcpImports class.

    Monday, August 31, 2009 12:24 PM
  • Hi,

    When you call the Invalidate() method, could you provide some code?

    Thanks,

    Wednesday, September 2, 2009 5:24 AM
  •   

    var bmp = new WriteableBitmap(frame.Size.Width, frame.Size.Height);
    //bmp.Lock();

    var decoder = new PngDecoder();
    var png = decoder.Read(file.Body.To<byte[]>().To());
    var decoded = png.Buffer.ToRaster(png.Width, png.Height);

    for (var x = frame.Size.Width - 1; x >= 0; x--)
    {
    for (var y = frame.Size.Height - 1; y >= 0; y--)
    {
    if (decoded[0][x, y] > 0)// || decoded[1][x, y] != 0 || decoded[2][x, y] != 0)
    bmp.Pixels[(y * frame.Size.Width) + x] = Colors.Black.ToArgb();
    }
    }

    bmp.Invalidate();
    //bmp.Unlock();
    return bmp;
     
    Wednesday, September 2, 2009 6:49 AM
  •  Any comments?

    Monday, September 7, 2009 9:47 AM
  • I'm also having problem with this all the time. Did you find a solution? This line of code always fails:

    Uri uri = new Uri("img0012.jpg", UriKind.Relative);
    BitmapImage bi = new BitmapImage(uri);
    WriteableBitmap wb = new WriteableBitmap(bi);

    Message "Object reference not set to an instance of an object."
    at MS.Internal.XcpImports.CheckHResult(UInt32 hr)\r\n  
    at MS.Internal.XcpImports.WriteableBitmap_CreateFromSource(WriteableBitmap wb, IntPtr ptrMemory, BitmapSource source, Boolean& bHasProtectedContent)\r\n  
    at System.Windows.Media.Imaging.WriteableBitmap..ctor(BitmapSource source)\r\n  
    at SLJpegTransparency.MainPage..ctor()

    Anyone?

    Tuesday, November 17, 2009 3:48 PM
  • Not sure but I think the BitmapImage has to be connected to an object, like an Image control for the WriteableBitmap to point to it.

    Tuesday, November 17, 2009 4:09 PM
  • If you mean like below with connected to an Image then unfortunately, it does not work either.

    Uri uri = new Uri("img0012.jpg", UriKind.Relative);
    BitmapImage bi = new BitmapImage(uri);
    _image.Source = bi;
    try
    {
       WriteableBitmap wb = new WriteableBitmap(bi);
    }
    catch (Exception ex)
    {
       string s = ex.Message;
    }

    Tuesday, November 17, 2009 4:58 PM
  • I am having the exact same error when certain conditions exist.  I am using SL4 and VS2010.

    Scenario: Button with an image.  The button shows a color selector for changing the font color of a label on the screen.  I want to be able to change the color on the image to reflect the color the user has selected.  Vice versa, if the user selects the label I want to change the color of the image to reflect the color of the label.

    Everything works great when the user selects the color after clicking the button.  However, I get your same error when the user clicks the label (this line errors out: image = New System.Windows.Media.Imaging.WriteableBitmap(source))

    I have pasted my code below..  Can someone PLEASE help me????

    Public Function ConvertToOldToNewColor(ByVal source As Object, ByVal c As Color) As System.Windows.Media.Imaging.WriteableBitmap

            Dim image As System.Windows.Media.Imaging.WriteableBitmap = Nothing
            If TypeOf (source) Is System.Windows.Media.Imaging.WriteableBitmap Then
                image = source
            Else
                'Errors out if user has not clicked on the ColorSelector (_treeChild is NOTHING)..  Appears to be a bug in someone elses code.
                image = New System.Windows.Media.Imaging.WriteableBitmap(source)
            End If
            If image IsNot Nothing Then
                Dim len As Integer = image.Pixels.Length
                'Pixels 192 - 255 have the color I want to use..
                For i As Integer = 192 To len - 1
                    System.Windows.Media.Imaging.WriteableBitmapExtensions.SetPixeli(image, i, c.R, c.G, c.B)
                Next
                image.Invalidate()
            End If
            Return image
        End Function

    Tuesday, February 1, 2011 5:26 PM
  • I am having the exact same error when certain conditions exist.  I am using SL4 and VS2010.

    Scenario: Button with an image.  The button shows a color selector for changing the font color of a label on the screen.  I want to be able to change the color on the image to reflect the color the user has selected.  Vice versa, if the user selects the label I want to change the color of the image to reflect the color of the label.

    Everything works great when the user selects the color after clicking the button.  However, I get your same error when the user clicks the label (this line errors out: image = New System.Windows.Media.Imaging.WriteableBitmap(source))

    I have pasted my code below..  Can someone PLEASE help me????

    Public Function ConvertToOldToNewColor(ByVal source As Object, ByVal c As Color) As System.Windows.Media.Imaging.WriteableBitmap

            Dim image As System.Windows.Media.Imaging.WriteableBitmap = Nothing
            If TypeOf (source) Is System.Windows.Media.Imaging.WriteableBitmap Then
                image = source
            Else
                'Errors out if user has not clicked on the ColorSelector (_treeChild is NOTHING)..  Appears to be a bug in someone elses code.
                image = New System.Windows.Media.Imaging.WriteableBitmap(source)
            End If
            If image IsNot Nothing Then
                Dim len As Integer = image.Pixels.Length
                'Pixels 192 - 255 have the color I want to use..
                For i As Integer = 192 To len - 1
                    System.Windows.Media.Imaging.WriteableBitmapExtensions.SetPixeli(image, i, c.R, c.G, c.B)
                Next
                image.Invalidate()
            End If
            Return image
        End Function

    Wednesday, February 2, 2011 1:19 PM
  • I FIXED IT!!!!

                Dim bitmapImage As New System.Windows.Media.Imaging.BitmapImage()
                bitmapImage.CreateOptions = Imaging.BitmapCreateOptions.None
                bitmapImage = ResourceHelper.GetBitmap(someURIString)
                image = New System.Windows.Media.Imaging.WriteableBitmap(bitmapImage)


    Reference this website for help(Thanks Schwammy):

    http://www.blog.ingenuitynow.net/Silverlight+Creating+A+WriteableBitmap+From+A+Uri+Source.aspx

    My images are in the silverlight app which get bundled into the XAP as a Resource.  So loading them from a regular URI was not an option.  Reference this website for information on how to load a bitmap resource:
    http://www.silverlightexamples.net/post/Load-Bitmap-Image-From-Resource-in-a-Single-Line-of-Code.aspx

    Maybe this is why you are still getting the error....

    Wednesday, February 2, 2011 5:21 PM