0

I know this question has been asked before, but i couldnt find an answer for my specific situation. Im trying to create new bitmap from a resource image.

private void button1_Click(object sender, EventArgs e)
    {

        string test = "meridian_am";
        string resources = "Resources.Properties.Resources." + test;

        var master = new Bitmap(Resources.Properties.Resources.master);
        var meridian_am = new Bitmap(resources);

        using (Graphics g = Graphics.FromImage(master))
        {
            g.DrawImageUnscaled(meridian_am, 114, 332);
        }
     }

for some reason, for the *var meridian_am = new Bitmap(resources)* im getting an invalid parameter error.. Ultimately, i would rather do a string concatenation on the fly, as in var meridian_am = new Bitmap(Resources.Properties.Resources. + test), but for some reason that wont work... Any ideas?

amartin94
  • 505
  • 3
  • 19
  • 35

2 Answers2

1

I'm no expert in C#, but... for var master = ... you're passing a resource that might well be an Image or something like that, while for var meridian = ... you're passing a string as the parameter, which should be the path to an accessible file. Hope that helps.

EDIT:

I'm talking this constructor versus this one.

LexLythius
  • 1,904
  • 1
  • 12
  • 20
  • They are both accessible file paths.. both use Resources as file paths – amartin94 Jun 29 '13 at 05:22
  • And i need them both to be resources, since its a Desktop app, and i want to compile the images in with the solution – amartin94 Jun 29 '13 at 05:25
  • You mean the value of `Resources.Properties.Resources.master` is itself a string as well, such that it contains something like "C:\...\myfile.png"? Otherwise you're comparing a packaged resource versus a string that should be the name of an **external** file accessible from the **executed** program – LexLythius Jun 29 '13 at 05:25
  • I dont even know.. the file is a C# resource, which was added in using VS resource UI. And Resources.Properties.Resources.master is how i point to that, however, i need to make it adjustable so it can allow for user input – amartin94 Jun 29 '13 at 05:36
1

Does Resources.Properties.Resources.master contain image bytes or string (path to file name)? In any case you can't pass raw string "Resources.Properties.Resources.meridian_am" to Bitmap constructor. It treats it as path to file, not your resource.

You should load data from resources by this name before. Something like that (if your resource contain image bytes):

var assembly = System.Reflection.Assembly.GetEntryAssembly();
var resources = assembly.GetManifestResourceStream("Resources.Properties.Resources." + test);
Vitaliy Shibaev
  • 1,420
  • 10
  • 24
  • It contains the actual image... if i used just the string, as a filepah, the image wont be compiled into the actual solution will it ? – amartin94 Jun 29 '13 at 05:41
  • now im getting errored with *Value of 'null' is not valid for 'stream'.* I ran a messageBox to check the value of it: * MessageBox.Show(Convert.ToString(resources));* and it came back as empty – amartin94 Jun 29 '13 at 05:44
  • Yes, of course, it will not be compiled if you reference to file path. So just use code from my comment, but be sure to use correct resource name (with full namespace declaration). – Vitaliy Shibaev Jun 29 '13 at 05:47
  • You pass incorrect resource name - GetManifestResourceStream returns null (can't find). Please look at this advices: 1. http://stackoverflow.com/questions/15276969/what-is-the-resource-name-for-a-manifest-resource-assembly-getmanifestresources 2. http://stackoverflow.com/questions/10726857/why-does-getmanifestresourcestream-returns-null-while-the-resource-name-exists-w – Vitaliy Shibaev Jun 29 '13 at 05:49
  • So once i've used your two lines, would i then just use *var meridian_am = new Bitmap(resources);*. I would wouldnt i ? – amartin94 Jun 29 '13 at 06:10
  • With code above you will get your image bytes in "resources" variable. Then you can pass it to Bitmap constructor. – Vitaliy Shibaev Jun 29 '13 at 06:17
  • Here is more detailed explanation: http://www.attilan.com/2006/08/accessing-embedded-resources-using.html – Vitaliy Shibaev Jun 29 '13 at 06:18