Combining a string and resource - c#

I have a dataset which contains some products and their image filenames (for example "test.png").
Images are loaded as resources. How can I set the image location properly?
DataRowView uRow = (DataRowView)comboBox1.SelectedItem;
DataRow row = uRow.Row;
pictureBox1.ImageLocation = Properties.Resources + row["logo"].ToString();

You can get a string resource by name like this:
string imageName = row["logo"].ToString();
// Strip off the extension, since it is not contained in the resource name.
imageName = Path.GetFileNameWithoutExtension(imageName);
pictureBox1.ImageLocation =
Properties.Resources.ResourceManager.GetString(imageName);
If you have stored the images themselves as resources, you can get them by name like this:
pictureBox1.Image =
(Image)Properties.Resources.ResourceManager.GetObject(imageName);

The ImageLocation property does not support using resources directly. From the MSDN Docs:
Gets or sets the path or URL for the image to display in the
PictureBox.
To load an image that is a resource, please see Change PictureBox's image to image from my resources?

If you set property Access modifier of Resources.resx file to the Friend or Public
then images or another resources can be accessed without hardcoding names.
Visual Studio will generate a class and properties for every resources
pictureBox1.Image = Properties.Resources.YourImage;

Related

Xamarin Forms - getting the file name of an image held in local storage

Is there any way to get the file name of an image source where that source is ImageSource.FromStream?
If I have 2 images in XAML, image01 and image02. The source for image01 is myImage and is included in the app bundle so I can get the name easily using string _source = image01.Source.ToString().
If I want the file name for image02 which resides in local storage as myImage2 how would I do that? image02.Source.ToString() returns File: n.Forms.StreamImageSource
[paraphrased] Can I get the file name from ImageSource.FromStream?
No. A Stream is an object which holds a load of bytes in memory. It doesn't store the location of where those bytes came from. The data inside a Stream could have come from a file, a web page, or any other source.
If you want to point to an image in your local storage, you will need to hard-code the folder location. Then you can search by filename extension. For example:
private const string MyStorageLocation = "/0/external/Pictures"; // Or wherever
private string[] GetAllPngImages()
{
return System.IO.Directory.GetFiles(MyStorageLocation, "*.png", System.IO.SearchOption.TopDirectoryOnly);
}
The string[] will contain a list of available images. You would then assign the source of image02 to one of those images using ImageSource.FromFile:
image02.Source = ImageSource.FromFile(images[0]);

Xamarin C# - How to reference a resource file programatically from a variable

I'm using Xamarin(C#) and trying to access the files in my Resources/drawable folder so that I can fill an ImageView with that picture. I have all the names of these resources(which are .png files) stored in an array and would like to loop through it to access each one.
I know you can access one file using Resource.Drawable.whateverTheNameIs but I don't want to hard code in the name, I want to be able to get it from a string variable. Is this possible? I tried the GetDrawables method but it's not finding it, and it also says its deprecated so I'd prefer the current way to do it.
Here's the exact code:
ImageView daButton = row.FindViewById<ImageView>(Resource.Id.theButtonThing);
Drawable d = myContext.Resources.GetDrawable(myContext.Resources.GetIdentifier(mItems[position], "button", myContext.PackageName));
daButton.SetImageDrawable(d);
Thanks!
you can find resource Id this way, it works for me:
int resID = Resources.GetIdentifier(mDrawableName, "drawable", PackageName);

two project in the same solution image url problem

I have two projects on my solution and I want to use other project pictures dynamically How can I link to other project picture (using imageurl property)
System.Web.UI.WebControls.Image imd = new System.Web.UI.WebControls.Image();
imd.Width = 220;
imd.Height = 215;
imd.ImageUrl = "~/Content/Images/Attachments/test.png;
pnlattachment.Controls.Add(imd);
To access an image from another project, you'd have to embed that image as an embedded resource into the assembly and access it using GetWebResourceUrl: http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getwebresourceurl.aspx
Or, if we are talking two web sites, you could use the ../../ notation to access the appropriate directory, since ~ only works in the context of the current virtual directory.
HTH.

How to programatically access images in a resource file?

I have 30 PNGs in a resource file and I would like to iterate around them inside a timer. This timer sets the form's background image to the next PNG in the sequence to produce a basic animation.
I can't find an easy way to enumerate the resource file and get at the actual images. I am also keen to keep the references to the images not fixed to their filenames so that updating the naming of the images within the Resource File would not require me to update this section of code.
Notes:
The images inside the resource file are named in sequence ('image001.png', 'image002.png', ...).
This resource file is used exclusively to store these images.
private void Form1_Load(object sender, EventArgs e)
{
var list = WindowsFormsApplication1.Properties.Resources.ResourceManager.GetResourceSet(new System.Globalization.CultureInfo("en-us"), true, true);
foreach (System.Collections.DictionaryEntry img in list)
{
System.Diagnostics.Debug.WriteLine(img.Key);
//use img.Value to get the bitmap
}
}
Here is a nice tutorial on how to extract embedded resources here on CodeProject, and here is how to use an image as alpha-blended, and shows how to load it into an image list. Here's a helper class to make loading embedded resources easier here.
Hope this helps,
Best regards,
Tom.
Assembly asm = Assembly.GetExecutingAssembly();
for(int i = 1; i <= 30; i++)
{
Stream file = asm.GetManifestResourceStream("NameSpace.Resources.Image"+i.ToString("000")+".png");
// Do something or store the stream
}
To Get the name of all embedded resources:
string[] resourceNames = Assembly.GetManifestResourceNames();
foreach(string resourceName in resourceNames)
{
System.Console.WriteLine(resourceName);
}
Also, check out the example in the Form1_Load function.
How about this forum answer that uses GetManifestResourceStream() ?

How to retrieve Image from Resources folder of the project in C#

i Have some images in resources folder in my project but i want to change the picture box from these resource files of the project
Consider using Properties.Resources.yourImage
Properties.Resources contains everything that you've added as a resource (see your project properties, resources tab)
Other than that, if you embed the images as resource in your project, you can get at them by calling GetManifestResourceStream on the assembly that you've embedded the images in, something like
Stream imgStream =
Assembly.GetExecutingAssembly().GetManifestResourceStream(
"YourNamespace.resources.ImageName.bmp");
pictureBox.Image = new Bitmap(imgStream);
Don't forget to mark the image as an embedded resource! (You'll need to set the build action for the image in its properties window)
If you're finding that you keep getting back null from GetManifestResourceStream, you may be giving the wrong name. (It can be hard to get the names right) Call GetManifestResourceNames on the assembly; that will give you back all the resource names, and you can find the one in the list that you need.
string img = null;
private void btnShow_Click(object sender, EventArgs e)
{
string imgName;
img = textBox1.Text;
imgName = "images/" + img + ".jpg";
if (imgName == null)
{
MessageBox.Show("no photo");
}
else if (imgName != null)
{
this.picBox1.Image = Image.FromFile("images/" + img + ".jpg");
}
}
Below is the Code to Fetch Image From Resources Folder. Normally we keep images in Resources Folder. but Sometime we have image name only with us. in that case you can Access images from Resources Folder using Image Name only.
Below Code will Demonstrate about it.
private System.Resources.ResourceManager RM = new System.Resources.ResourceManager("YourAppliacationNameSpace.Properties.Resources", typeof(Resources).Assembly);
PbResultImage.Image = (Image)RM.GetObject(YourPictureName);
YourAppliacationNameSpace means name of your Application.
YourPictureName means the Picture you Want to access from Resources Folder. but Picture name must be without Extension e.g. (PNG, GIF, JPEG etc)
hope i will be beneficial to some one.
Thank you.
Right click on the project. Go to Resources tab. Select the existing option and add the image.
Now access in the code by
Image = Properties.Resources.ImageName
Worked for me:
(Bitmap) Properties.Resources.ResourceManager.GetObject("ImageName");
this works for your control too, if you need to call your control by a string or concatenated reference.
Just call the control type first (in this case, a picturebox). Assume i=some number and d=some string:
var i = <some number>;
var d = <some string>;
((PictureBox)this.Controls["pictureBox" + i.ToString()]).Image = (Image)Properties.Resources.ResourceManager.GetObject("dice" + d);
Now it's Possible
pictureBox.Image = Properties.Resources.yourImagename;

Categories

Resources