• 0

The Bitmap.FromResource() function


Question

Hi all. I'm trying to use the Bitmap.FromResource() function in C#, but I don't understand the first part of the syntax -> 'System.IntPntr hInstance'.

Could someone explain me what I have to fill in there. I guess it must be some sort of pointer to my program. I tried a couple of things, including 'IntPtr.Zero', but it doesn't work. The program gives me the following error:

System.ArgumentException: Invalid parameter used

Is there someone who knows how to fix this? Thx in advance.

Link to comment
https://www.neowin.net/forum/topic/369511-the-bitmapfromresource-function/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

HINSTANCE and HMODULE are the base addresses of any exe/dll/images loaded into memory. Are you loading the resource from your executable? If yes, you can use 0 (IntPtr.Zero?). If you are loading from a dll, how are you loading the dll?

  • 0

Nope, it doesn't work. I get the following error returned:

System.ArgumentException: Source 1.bmp cannot be found in the class Elvish_Dance.Form1.

at System.Drawing.Bitmap..ctor(Type type, String resource)

at Elvish_Dance.Form1.timer_Tick(Object sender, EventArgs e) in c:\documents and settings\djtrauma\mijn documenten\visual studio projects\elvish dance\elvish dance\form1.cs:line 146

at System.Windows.Forms.Timer.OnTick(EventArgs e)

at System.Windows.Forms.Timer.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr idEvent, IntPtr dwTime)

Part of my code:

Bitmap _bmp;

 ?	switch(gen)
 ?	{
 ? ?case 1:
 ? ?	_bmp = new Bitmap(GetType(), "1.bmp");
 ? ?	screen.Image = _bmp;
 ? ?	break;
 ? ?case 2:
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ...

  • 0

One solution would be this:

Select the resource in your Solution Explorer and change the "Build Action" property to "Embedded Resource".

When a resource is added like that, it can be accessed from the Assembly.

public Stream GetEmbeddedFile(string Name)
{
     try
     {
          Assembly asm = Assembly.GetExecutingAssembly();
          return asm.GetManifestResourceStream(Name);
     }
     catch
     {
          return null;
     }
}

This method returns any embedded resource as a stream, or null when an exception occurs.

The Name parameter should include the AssemblyName, something like: MyApplication.1.bmp

A Bitmap can be created by using Bitmap.FromStream(...);

Hope this helps :)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • How much smaller is the battery in the Edge?
    • Samsung Galaxy S25 Edge sales are apparently terrible by Usama Jawad After much fanfare, Samsung officially launched the Galaxy S25 Edge last month, marketing it as the slimmest phone in the series. The handset is 5.8mm thick and weighs 163g, hosts a 200MP primary lens and a 12MP ultra-wide sensor, and a Snapdragon 8 Elite Mobile Platform for Galaxy processor - the same one powering the S25 series. However, despite all of these features backed by Samsung's marketing effort, it seems like the handset isn't performing well in terms of sales. A report from South Korean outlet The Elec claims that the S25 Edge is is selling fewer units than what Samsung expected. This has resulted in Samsung lowering the number of units it plans to produce in June. It is important to keep in mind that it has been a little over a month since the S25 Edge came out, and since handsets are expected to sell well a few months within their launch date, this isn't a particularly promising sign. An earlier rumor had claimed that Samsung will be increasing the production of its foldable units and will phase out the upcoming S26 Plus in favor of the S26 Edge. However, the latest news out of South Korea might make Samsung rethink its strategy. The latest report hasn't shed light on why the S25 Edge is underperforming in sales. This could be due to the prohibitive price tag of $1,100 (keep in mind that the S25+ costs $1,000), but this is just speculation at this point due to lack of official evidence. Those interested in still getting their hands on the S25 Edge can grab the top-of-the-line 512GB model at a 20% discount right now. This brings the price down from $1219.99 to just $969.99. Perhaps this significant discount so early after launch is also an indicator about the poor sales performance of the handset. Source: The Elec
    • Someone not paying the BBC license at home is a minor inconvenience to BBC. The real problem are these Ai companies STEALING data through scraping and then selling it under their product. Just pirating something for personal use is nowhere near as bad as stealing it to make corporate profits.
  • Recent Achievements

    • First Post
      emptyother earned a badge
      First Post
    • Week One Done
      Crunchy6 earned a badge
      Week One Done
    • One Month Later
      KynanSEIT earned a badge
      One Month Later
    • One Month Later
      gowtham07 earned a badge
      One Month Later
    • Collaborator
      lethalman went up a rank
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      670
    2. 2
      ATLien_0
      273
    3. 3
      Michael Scrip
      218
    4. 4
      +FloatingFatMan
      165
    5. 5
      Steven P.
      163
  • Tell a friend

    Love Neowin? Tell a friend!