I have a warning that I can;t get rid of:

Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

I am pretty sure it is becasue I have not dimentioned the variable but how do you dimention an IO.memorystream.

Here is the code:

Photograph1.Image = Photograph1.Image.FromStream(New IO.MemoryStream(arr))

Any help would be greatfull. The programms runs and works, its just got those annoying warnings.

Dani AI

Generated

Short summary and practical follow-up for based on the replies from and .

The IDE warning means a Shared (static) member is being accessed through an instance. VB will call the Shared method on the type, and the qualifying expression (the instance you wrote) will not be evaluated — so the code works but the IDE flags it as poor style and potentially misleading. As noted, call the shared method on the Image type itself to remove the warning and make intent clear.

A few important runtime and resource notes that weren’t fully covered above:

  • When you load an Image from a Stream, GDI+ may depend on that Stream for the lifetime of the Image. If you want to close or dispose the stream immediately, create an independent copy of the image (for example by constructing a fresh Bitmap from the loaded image or using Clone) and then dispose the intermediate image and the stream. That avoids keeping the MemoryStream open and prevents file/handle locking issues.
  • Always guard against bad data: check that the byte array is not null/empty and wrap image creation in try/catch. GDI+ throws exceptions (commonly an OutOfMemoryException) when the data is not a valid image format.
  • Manage GDI resources: when replacing a PictureBox.Image, dispose the old Image reference to avoid leaks (store old reference, set the new image, then dispose the old one). Use Using blocks for streams where appropriate after you have an independent image copy.

Checklist to fix and harden the code: call the shared method on the Image class, create a MemoryStream from the byte array, make a copy so you can dispose the stream, dispose the previous PictureBox image, and wrap the operation in validation/exception handling. This removes the warning and prevents resource and locking problems.

Recommended Answers

All 5 Replies

I know that to load an image from a file I do

Photograph1.Image = New Bitmap(filename)

I can't recommend what you should do without knowing what arr is or what it contains.

Sorry, didn't explain very well. arr is a binary data file stored in a database.

What is odd is that loading from a file (as your example) doesn't give the warning, but loading from io.memorystream does. Why?

So if arr already contains binary data why do you need the IOStream? And if you do need the IOStream then try

    Photograph1.Image = New Bitmap(New IO.MemoryStream(arr))

It's not my area of expertise so I'm just throwing out suggestions and hoping that one sticks.

arr is a binary data file stored in a database

Assuming that this means that "arr" is a byte array that has been loaded from a database, then

change: Photograph1.Image = Photograph1.Image.FromStream(New IO.MemoryStream(arr))

to: Photograph1.Image = Image.FromStream(New IO.MemoryStream(arr))

FromStream is a shared method on the Image class. The IDE does not like you accessing a shared method/field/property on an instance eventhough as it works as you have found out. In this case, the instance is the Image property on Photograph (assuming Photograph is a PictureBox).

Thank you so much you guys, both options worked a treat so I went with TnTinMN just because it seemed the right way to go.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.