I am trying to load an image to picture box the below way worked with me.
image1.Image = Properties.Resources._0;
what it i want make the code reading from resources as variable name like the way below.
var imagename = _0;
image1.Image = Properties.Resources.imagename;
Thanks
Use the ResourceManager and pass it a string:
var imageName = "_0";
image1.Image = (Image)Properties.Resources.ResourceManager.GetObject(imageName);
Related
If I have some resources, eg card1.png, card2.png, etc,
I want to them load them into a picBox, but only load the correct image
eg, something like
int cardNum = rn.Next(0,cardMax);
picCard.Image = Properties.Resources."card"+cardNum+".png";
Obviously that doesn't work, but how would I do what I am trying to do (load the correct resource after building the resource name into a string)
Instead of the generated properties in the Resources class use the ResourceManager directly:
string resName = $"card{cardNum}.png"; // Check the correct name in the .resx file. By using the wizards the extension is omitted, for example.
picCard.Image = (Image)Properties.Resources.ResourceManager.GetObject(resName);
Try using:
var rm = new System.Resources.ResourceManager(((System.Reflection.Assembly)System.Reflection.Assembly.GetExecutingAssembly()).GetName().Name + ".Properties.Resources", ((System.Reflection.Assembly)System.Reflection.Assembly.GetExecutingAssembly()));
Image img = (Bitmap)rm.GetObject("resourcesimagename.jpg");
I would like to set source of my image object programmatically in Xamarin. I have a text file and it has more than 200 file names. I read it line by line. So see what i want to do (not in Xamarin.Forms)
string[] MyFileNames = new string[] { };
MyFileNames = ReadedTextFileVariable.Split(System.Environment.NewLine);
ImageObject.ImagePath = MyFileNames[2]; //its an example. i want to put a string path. uri is not working
Thanks in advance.
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?
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.
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);
How can I use an image from resources as a footer in created excel file?
this definately will not work:
xlWorkSheet.PageSetup.CenterFooterPicture = Properties.Resources.stopka;
since:
Cannot implicitly convert type 'System.Drawing.Bitmap' to Microsoft.Office.Interop.Excel.Graphic'
Ok this works:
xlWorkSheet.PageSetup.CenterFooterPicture.Filename = Application.StartupPath + "\\stopka.png";
xlWorkSheet.PageSetup.CenterFooterPicture.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
xlWorkSheet.PageSetup.CenterFooterPicture.Width = 590;
xlWorkSheet.PageSetup.CenterFooter = "&G";
But it is not what I needed. I would like to get the image from project resources not from application folder.
This works:
System.Reflection.Assembly CurrAssembly = System.Reflection.Assembly.LoadFrom(System.Windows.Forms.Application.ExecutablePath);
System.IO.Stream stream = CurrAssembly.GetManifestResourceStream("Oferty_BMGRP.Resources.stopka.png");
string temp = Path.GetTempFileName();
System.Drawing.Image.FromStream(stream).Save(temp);
xlWorkSheet.PageSetup.CenterFooterPicture.Filename = temp; //Application.StartupPath + "\\Resources\\stopka.png";
xlWorkSheet.PageSetup.CenterFooterPicture.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
xlWorkSheet.PageSetup.CenterFooterPicture.Width = 590;
xlWorkSheet.PageSetup.CenterFooter = "&G";
the image "stopka.png" had to be set as embeded resource.
Since microsoft doesn't provide the set option for CenterFooterPicture property
The following is the documentation available in MSDN
CenterFooterPicture - Returns a Graphic object that represents the picture for the center section of the footer. Used to set attributes about the picture.
Link to Refer
Im making my own mp3 tagger, and everything is fine so far. Although im stuck reading the album art tag.
I would like to know how to display the cover in a C#.NET picture box, but everything iv seen about that particular tag is confusing me.
I know i can get tags from files like this
txtAlbum.Text = currentFile.Tag.Album;
but all i need to do is grab the picture from the file and whack it in a picturebox. Then i would like to know how to write a picture (jpg, png) into the file and overwrite the existing one.
Any help would be greatly appreciated, and thank you for your valued time.
Try this
TagLib.File tagFile = TagLib.File.Create(path);
IPicture newArt = new Picture(tmpImg);
tagFile.Tag.Pictures = new IPicture[1] {newArt};
tagFile.Save();
EDIT
var file = TagLib.File.Create(filename);
if (file.Tag.Pictures.Length >= 1)
{
var bin = (byte[])(file.Tag.Pictures[0].Data.Data);
PreviewPictureBox.Image = Image.FromStream(new MemoryStream(bin)).GetThumbnailImage(100, 100, null, IntPtr.Zero);
}
here's my quick and short solution for that problem:
var file = TagLib.File.Create(filename);
var bin = (byte[])(file.Tag.Pictures[0].Data.Data);
imageBox.Image = Image.FromStream(new MemoryStream(bin));