• 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

    • Or, it is Apple simply overinflated the screens.
    • Is it that easy though? WhatsApp is the default way the majority message in a lot of countries these days. I would prefer Signal to be as popular as WhatsApp and probably could get a few people to use it, most people are probably going to stick with WhatsApp sadly. Which leaves SMS or Facebook Messenger as alternatives a lot of people also have. (Here anyway, I know iMessage, LINE and WeChat dominate in certain parts of the world). It annoying Meta purchased WhatsApp all those years ago.
    • Do they tell Google not to scrape their content via something like robots.txt? Do they specify anywhere that certain agents aren't to scrape? If not, tough. Plus there's no obligation on anyone's part to adhere to any directives that might be in this file anyway...
    • DMDE 4.3.5.823 Beta by Razvan Serea DMDE is a software designed to effectively recover lost data. It retrieves files and folders swiftly and stores them in the user-defined location. It is an easy to use yet powerful tool that will assist both novice and experienced users in getting back lost files in just a few simple steps. Free Edition includes all basic features but a single recovery operation recovers up to 4000 files in the current panel only (you should first open a subdirectory in the current panel and then recover files in the panel). In paid licenses there is no this restriction, and recovery of nested directories is allowed. Can paid versions recover more files than the free version of DMDE? If a file cannot be recovered in the DMDE Free Edition (or it is damaged after recovery) the same will occur in the paid versions. DMDE paid versions are capable of recovering the same files. The only difference is that paid versions can recover all found files in one go, as well as restore the directory structure presented in the free version. Professional Edition provides additional features: rights to provide data recovery services portable use on different computers one-time activation on client computers (including remote use) data recovery reports (include logs and file checksums) read support for E01 disk image files using logs when copying a disk (resume copying, multiple passes) customizable I/O handler script recovery of NTFS alternate data streams DMA access in DOS (for ATA interface) DMDE key features: Portable run without installation Support for NTFS, FAT12/16, FAT32, exFAT, ReFS, Ext2/Ext3/Ext4, btrfs, HFS+/HFSX, APFS Thorough FS and Raw scan, FS reconstruction for data recovery in complex cases Simple partition manager for express search, diagnostics, and restoration of partitions Disk cloning and disk image creating, including I/O error handling, reverse copying, and other features RAID constructor for virtual RAID reconstruction supporting levels RAID-0, RAID-1, RAID-4, RAID-5, RAID-6, delayed parity, custom striping, JBOD/spanned disks; automatic calculation of RAID configurations Cluster map to investigate file allocation Disk editor compatible with the most recent Windows versions which allows viewing, editing, and navigating through different disk structures using built-in and custom templates NTFS tools to work bypassing NTFS driver (copy, delete file, create, repair directory) Support for various device I/O interfaces and settings to work with damaged devices, disk images, NTFS compression and encryption, national names, large disks, large files, large sectors, and other features DMDE 4.3.5.823 Beta changelog: Expanded built-in signatures for RAW search functionality Added file list export to HTML format (DMDE Professional Edition only, view sample) Improved handling of I/O errors with selective skipping by error code Enabled preview support for additional image (graphic) file types (Windows only) Improved extfs reconstruction when copies of superblocks with group descriptors are found Fixed potential hang during Btrfs volume reconstruction Resolved issue with cluster list creation when subfolders are present Other improvements and fixes Download: DMDE 64-bit | 2.4 MB (Free, paid upgrade available) Download: DMDE 32-bit | 2.0 MB Link: DMDE Home Page | DMDE Manual | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • The BBC might have gone about this the wrong way, but if there is a revenue sharing program then they and all other "providers" of data should be included in the plan.
  • Recent Achievements

    • 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
    • Week One Done
      Wayne Robinson earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      679
    2. 2
      ATLien_0
      276
    3. 3
      Michael Scrip
      220
    4. 4
      +FloatingFatMan
      170
    5. 5
      Steven P.
      164
  • Tell a friend

    Love Neowin? Tell a friend!