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;
Related
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;
I'm trying to make a Media Player where you can choose either to render a file from url or local disc. I had no problem making it to open and render url file
void LoadVideo_Click(object sender, RoutedEventArgs e)
{
LoadVideo.IsEnabled = false;
mediaElement.Source = new Uri(path, UriKind.Absolute);
With string path = "http://www.blablabla.com/movie.wmv"
The problem occurs when I'm trying to specify local disc file path(as "c:\movie.wmv" or #"c:\movie.wmv"). It simply doesn't work that way.
As far as I have read, you don't have direct access to files on your hard drive besides those which already are in the project directory. What I want to do is:
use Dialog Box to choose a file to open
save the path of file into string and transfer it to MediaElement.Source
Unfortunately, I don't have a clue how to do it. I would be grateful for any advices.
Here you go, this should do the trick:
OpenFileDialog fdlg = new OpenFileDialog(); //you need to use the OpenFileDialog, otherwise Silverlight will throw a tantrum ;)
fdlg.Filter = "MP4 Files|*.mp4|AVI files|*.avi"; //set a file selection filter
if (fdlg.ShowDialog() != true) //ShowDialog returns a bool? to indicate if the user clicked OK after picking a file
return;
var stream = fdlg.File.OpenRead(); //get the file stream
//Media is a MediaElement object in XAML
Media.SetSource(stream); //bread and butter
Media.Play(); //no idea what this does
Here's an extensive example on how to use the OpenFileDialog. As for the MediaElement, you can see in the code above all you needed was the SetSource() method (as opposed to the Source property).
net/C# application I have list of items.
In the code behind:
I want to assign a picture from my local resources for each item. the items name and the picture names are the same.
The pictures are all in a "image" folder in my project.
Example of how I assign a picture to an item:
Item1.PictureUrl = "images/items/" + item1.Name + ".jpg";
I have items that don't have pictures. I want to assign for them a default picture.
I tried to check if the picture exists using this:
foreach(ObjectItem item in ListOfItems)
{
if(File.Exists("images/items/"+item.Name+".jpg"))
item.PictureUrl = "images/items/"+item.Name+".jpg";
else
item.PictureUrl= "images/Default.jpp";
}
But the File.Exists method is always returning false, even if the picture exist.
I also tried to use '\' instead of '/' but didn't work
How can I do it?
Thank you for any help
You need to convert the relative file path into a physical file path in order for File.Exists to work correctly.
You will want to use Server.MapPath to verify the existence of the file:
if(File.Exists(Server.MapPath("/images/items/"+item.Name+".jpg")))
Also, when you use Server.MapPath, you should usually specify the leading slash so that the request is relative to the web application's directory.
If you don't provide the leading slash, then the path will be generated relative to the current page that is being processed and if this page is in a subdirectory, you will not get to your images folder.
.Net Core Solution
1- Write this at the top of your View (Injecting IHostingEnvironment here);
#inject Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnv
2- Write this to the place of image(Doing existence check);
var path = System.IO.Path.Combine(hostingEnv.WebRootPath, "MyFolder", "MyImage.jpg");
if (System.IO.File.Exists(path))
{
<img class="img-fluid" src="~/MyFolder/MyImage.jpg" alt="">
}
var path = $#"C:\Fotos\Funcionarios\1.Png";
FileInfo file = new FileInfo(path);
if (file.Exists.Equals(true))
{
//faz algo
}
I have gotten a program Im working on to load some pictures and display them within a listview after using a openfiledialog. What I am looking to do now is take this one step further and auto-load the images from a directory 'icons' within the application directory. Im not too sure how to go about it, So Im going to paste my current code here, and work it from there...
private void loadImageLibraryToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (openFileDialog1.FileNames != null)
{
for (int i = 0; i < openFileDialog1.FileNames.Length; i++)
{
addImage(openFileDialog1.FileNames[i]);
}
}
else
addImage(openFileDialog1.FileName);
}
}
private void addImage(string imageToLoad)
{
if (imageToLoad != "")
{
imageList1.Images.Add(Image.FromFile(imageToLoad));
listView1.BeginUpdate();
listView1.Items.Add(imageToLoad, baseValue++);
listView1.EndUpdate();
}
}
Edit to Clarify: The code provided shows how to load and show the images in a listview control. What Im looking to do now is upon starting the app, load the images automatically from a folder in the programs directory, and then display them in the listview.
Off the top of my head with no IDE so there may be mistakes! try this
var files = System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\icons")
files will be an array of strings containing all the files in the directory which you can then loop as you have above using the array
openFileDialog1.FileNames
The \ may not be required before icons, I can't remember if GetDirectoryName drops the trailing \ from the path or not.
you can also pass a filter to GetFiles to only return certain file types.
HTH
EDIT: I have edited the code above to use
System.Windows.Forms.Application.ExecutablePath
rather than
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
On testing the code now I have access to an IDE it seems the CodeBase property prepends the path with file:/// which caused my IDE to blow up with an error saying
URI formats are not supported
the code now works in my IDE, You need to make sure your icons Directory is in the same directory as your executable so in my case ....bin\debug\
Give this a try and if it still fails let me know!
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() ?