|
I am using Bitmap imgBold = Bitmap.FromFile("bold.bmp"); to retrieve a bitmap file, but when I try to compile the program, it says that it can't directly convert an Image object into a Bitmap object. It appears as though the Bitmap.FromFile method returns an Image object... Is there a method of the Image class that will allow me to convert the Image into a Bitmap ? The reason I need it to be a bitmap is that I need to set the transparency color. You can't do this with an Image object.
-----
Note: *** Never give me an answer have anything to do with Visual Studio. I don't have this program, and it'll be that way for a long, long time. ***
|
|
|
|
|
Why don't you simply cast the Image to Bitmap?
Bitmap imgBold = (Bitmap)Bitmap.FromFile("bold.bmp");
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Try this:
Bitmap imgBold = (Bitmap) Bitmap.FromFile("bold.bmp");
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Hi Max,
you can use a stream instead of FromFile, just open a file stream and do:
Bitmap imgBold=new Bitmap(stream);
Luc Pattyn
|
|
|
|
|
You should never do a direct cast. Do this instead
Bitmap bm = Bitmap.FromFile("Blah") as Bitmap;
the 'as' operator will return null if the object is not a valid target for the cast. A cast will just blow up.
In this case, you know it will work, but it's just neater to get into the habit of doing it this way.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Christian Graus wrote: You should never do a direct cast. Do this instead
Bitmap bm = Bitmap.FromFile("Blah") as Bitmap;
the 'as' operator will return null if the object is not a valid target for the cast. A cast will just blow up.
Well, it's not always the best idea to postpone the error message as long as possible.
The code is more maintainable if an error occurs as close to the source as possible. If a cast doesn't work, it's easier to find the cause if the error message comes at the cast, than if it comes somewhere later in the code because of a reference mysteriously turning null.
Using as only serves a purpose if you actually check for null afterwards.
Also, in this case if the cast fails, you will lose the reference to the original object, which means that you won't be able to dispose it.
---
single minded; short sighted; long gone;
|
|
|
|
|
Guffa wrote: Using as only serves a purpose if you actually check for null afterwards.
Obviously, yes.
Guffa wrote: Also, in this case if the cast fails, you will lose the reference to the original object, which means that you won't be able to dispose it.
Oh, good point...
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Guffa wrote: Also, in this case if the cast fails, you will lose the reference to the original object, which means that you won't be able to dispose it.
Wouldn't I also loose the reference to the original object if the cast succeeds and therefore dispose it before doing this anyway.?
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Stefan Troschütz wrote: Wouldn't I also loose the reference to the original object if the cast succeeds
No. The type of the reference variable doesn't matter. A reference is a reference. If you want a reference to the object as the original, you can just cast the reference to the actual type.
---
single minded; short sighted; long gone;
|
|
|
|
|
Ah, I think I misunderstood your post. I thought with original object you were refering to the object that was referenced by the Bitmap variable before doing Bitmap bm = Bitmap.FromFile("Blah") as Bitmap but of course that is nonsense as the variable is declared in this line. So with original object you mean in this special case the Image object returned by FromFile , right?
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Stefan Troschütz wrote: So with original object you mean in this special case the Image object returned by FromFile, right?
Right.
---
single minded; short sighted; long gone;
|
|
|
|
|
becarfule that Bitmap class is sealed.so u cant create an object.just u should use Image class.
|
|
|
|
|
You are totally wrong and completley confused on what the OP is doing.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
You are totally wrong and completley confused on what the OP is doing.
what's wrong with me? Bitmap is a sealed class.can explain u me more?
|
|
|
|
|
sealed does not mean that you cant create an instance, of course you can do
"new Bitmap(...)". It does mean you cannot create a class that inherits from it.
Luc Pattyn
|
|
|
|
|
hi.
yes.Now i understand what a big mistake that i had make!
|
|
|
|
|
Hello,
Can I write a c++ code on my c# project?
I mean if I have an array (a) on c# and I want to make something with this array but on c++ code how can I do that?
I mean How can I mix the two languages togather?
SnaidiS(Semion)
|
|
|
|
|
Semion_N wrote: Can I write a c++ code on my c# project
No. A project can only be written in a single language, be it C++, C#, VB.NET, ... But, there's nothing stopping you from having multiple projects in your Solution, each written in a seperate language.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
You can mix languages in a solution, just write your C++ code in a C++/CLI dll and call it from C#.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
This is how I understood it (and I recall I once did a small but successful test
combining C#, VB.NET and J#):
You can create a dll in one .NET language,
and another dll or exe in another .NET language.
for each of these you need a project (that is possible in a single solution).
Now the second project can call on the dll from the first project, and they can
exchange all "CLR types" which covers most of the data types in a .NET language
(I think the biggest limitation is on VB.NET, not sure tho).
Luc Pattyn
|
|
|
|
|
Thank you all for your answers.
Do I have to make a dll from the other projects if I want to use the classes and ect on my C# project?
SnaidiS(Semion)
|
|
|
|
|
Hello,
perhaps someone knows the Hyperbar example from the Microsoft Expression blend beta...
I want to ask, if someone knows how to implement it in .Net 2.0...or knows similar implementations to look at.
Please don't ask, how the hyperbar works and looks like.I think my english is not enough to explain that.
Screenshot here
There a 11 items in the hyperbar on the screenshot. Which are visible depends on the mouse position.
thanks
|
|
|
|
|
You can do it. I've done it in MFC. The trick is, you need to float a frameless window over the toolbar, in MFC I grabbed a screenshot and made it the background to make the window invisible. In C#, you can probably just make it transparent. Then you can draw your enlarged buttons on this window as you move the mouse over it.
In other words, you need to write a custom control, but it can be done.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
|
I know what a hyperbar is, I wrote one using MFC.
The zoom is the issue, you need a paint area bigger than the toolbar that owns the images. Which is why you create an invisible window.
This video is of the exact same WPF sample that I used as a source when I was writing my MFC version.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|