Question on how to access bitmap: Public Function getBitmap() As Bitmap

M

Michael Murphy

At the risk of being publically humiliated, might I ask the following
simple question.

I have a solution with one project. This project, WindowsApplication1,
has one "form" and one "bitmap" (the bitmap created by right clicking
on the project, selecting add new item, then add bitmap file). The
project explorer says my bitmap is called "Bitmap1.bmp".

If I have a function in my project declared as

Public Function getBitmap() As Bitmap

How do I return this bitmap using this function?

I have looked at the the contructor for "Bitmap" and it has 12
versions, one of which is to pass the filename. I don't want to do
this because then it says my bitmap has to be in a certain location on
the disk - I just want to reference the bitmap already in the project
but can't see how to do it without needing to know the filename
(either directly or through a stream or input reader).
So am I missing something and it isn't easy?
Thank you
Michael
 
A

anonymous

Dim objBitmap as bitmap

objBitmap = New Bitmap
(System.Reflection.Assembly.GetExecutingAssembly.GetManife
stResourceStream("WindowsApplication1.Bitmap1.bmp"))

'what the code does is the following
'opens the application
'creates a stream to read the resource
'reads the resource belonging to
namespace 'WindowsApplication1' named 'Bitmap1.bmp'

Ensure that the resources is not simply added to the
project, but an EMBEDDED RESOURCE (right click on the bmp
file in solution explorer, properties, and ensure it's
embedded).

Regards
 
M

Michael Murphy

Thanks a lot. This worked fine. But ... and there's always a but, it
means I have to include a string literal
"WindowsApplication1.Bitmap1.bmp" to reference the bitmap. I would
rather refer to it by a name, so that if the name changed in one place
and not another, the /*compiler/* would not let the program compile.
The way it is coded here means that if I changed my window application
name, or changed the bitmap name from say "Bitmap1.bmp" to
"SendButton.bmp", it mightn't work anymore (unless me, or everyone
else modifying my program, also remembered to change it everywhere
carefully). Can you reference the bitmap resource by a name?
Thank you
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top