New Bitmap causing exception

  • Thread starter Thread starter HW
  • Start date Start date
H

HW

Hi, I'm getting a SystemUnAuthorised exception with the following bit
of code, separated out for clarity

Dim memStr As MemoryStream

memStr = New MemoryStream(imageptr)

picBox.Image = New Bitmap(memStr) <- exception here



Strangely it doesn't get caught by the Try Catch block but only by the
debugger. In the call stack

window it seems to be in mscorlib.dll Sytem.IO.memoryStream.GetBuffer.
imageptr is obtaimed from reading a

an image file which i've checked is not readonly. So why is the BitMap
constructor call throwing this

exception.
 
Please ignore last post, i still have this exception. Definitely something
up with Bitmap constructor call
 
imageptr is Byte array, basically i have a byte array and i'm converting
to Bmp using New Bitmap call and am getting UnAuthorised Exception
 
It's a byte array of what type of actual data? Is the source a BMP, JPG,
GIF, etc?

-Chris
 
Hi Chris, the source is a BMP. I've now found out that the

MemoryStream class has several overloads and involves a call to GetBuffer
which raises this exception if the buffer is not PUBLICLY VISIBLE

By using this version
Public Sub New ( _
buffer As Byte(), _
index As Integer, _
count As Integer, _
writable As Boolean, _
publiclyVisible As Boolean _
)and settig publiclyVisible to TRUEthe exception has now gone. This exception was killing my app performance wiseand is not caught using try,catch exception handling, i only spotted using debuggerby turning on exceptions, plus it doesn't seemto actually functionally make any difference using the wrong version of the Memorystream class overload. Why do Microsoft let this go through if you use the wrongversion, it just allows bugs to get through.
 
Back
Top