C# Adding Image to a Label - c#

Now i have to developing a WindowsForm using Visual C# 2010, What I need to be able to do is on a label make their be an image.
I have got the images included in the project/bin/Debug/ in a folder named "images"
Image img = Image.FromFile("PR001.jpg");
Label lblImage = new Label();
lblImage.Parent = this;
lblImage.Image = img;
lblImage.Size = new Size(img.Width, img.Height);
i need only file with extension (*.jpg)
can someone help me ?

Since your images are in the "images" folder, you have to modify this line
Image img = Image.FromFile("PR001.jpg");
to
Image img = Image.FromFile("images/PR001.jpg");
Note: The original line would search the file in "debug" folder, where your program executable (.exe) file is located.

You should take a look at the Label.BackgroundImage property.
And this links describes what you are looking for:
http://www.c-sharpcorner.com/uploadfile/mahesh/label-in-C-Sharp/
http://www.java2s.com/Tutorial/CSharp/0460__GUI-Windows-Forms/AddimagetoLabel.htm

This works for me:
Label ilabel = new Label(); // create a label
Image i = Image.FromFile("image.png"); // read in image
ilabel.Size = new Size(i.Width, i.Height); //set label to correct size
ilabel.Image = i; // put image on label
this.Controls.Add(ilabel); // add label to container (a form, for instance)
https://stackoverflow.com/a/16888310/470868

Related

Clicking on programmatically added pictureBox controls

I have a Winforms application where I programmatically add a number of pictureBox controls and load each one with an image.
How can I link each pictureBox control with a video file so I can run that video by clicking on that pictureBox control?
This is how I add my pictureBox controls:
if (ofd.ShowDialog() == DialogResult.OK)
{
file_address = ofd.FileName;
}
var picture1 = new PictureBox
{
Name = "pictureBox",
Size = new Size(160, 200),
Location = new Point(x, y),
SizeMode = PictureBoxSizeMode.Zoom,
Image = Image.FromFile(file_address),
};
this.Controls.Add(picture1);
I've managed to add click event handler:
picture1.Click += delegate
{
// Do something
}
You can use the Tag property to store the file_address.
this.picture1.tag = file_address;
when you want to play the video by the picture box file name, get the file name by
string file_name = (string)this.picture1.tag;

Add image to DataGridView using DataGridViewImageColumn

I am trying to add an image in a DataGridView using the following code
DataGridViewImageColumn Editlink = new DataGridViewImageColumn();
Image image = Image.FromFile("Images\\Edit.png");
Editlink.Image = image;
Editlink.HeaderText = "Edit";
Editlink.DataPropertyName = "lnkColumn";
Editlink.Width = 40;
In the above code
Image image = Image.FromFile("Images\\Edit.png");
It throws an error saying
File not found
When I changed the FromFile path to "C:\\Test\\Images\\Edit.png", it works.
How can I achieve the same result without using actual path?
Please use Add Resource to add Images to local Resource folder and then call it by file name.
The question is a duplicate of this
filenotfound-when-i-use-image-fromfile

How to set button backgroundimage in c#?

I'm new in this field. I'm working in windows application, I create panel and adding the buttons inside the panel. i want to retrieve the image from database and set to the background image in button.
This is my code,
FileName = objDR["Photopath"].ToString();
byte[] data = Encoding.UTF8.GetBytes(FileName);
MemoryStream ms = new MemoryStream(data);
image = new System.Drawing.Bitmap(ms);
Buttons[i].BackgroundImage = image;
initially put BackgroundImage property ON
OR
You can Code :
button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Image));

Image Control in Wpf showing previous image which was deleted

I have an image control which is showing the preview image. If the user delete the image (which resides in folder) it should show the newly taken image.
but the image control shows the deleted image instead of showing new image.
// clear the image source before deleting the image.
// save image in the directory
public string globalFilePath;
int imageCount = Directory.GetFiles(imgDir, "*", SearchOption.TopDirectoryOnly).Length;
string filePath = Path.Combine(imgDir, "IMAGE_" + ++imageCount + ".jpeg");
globalFilePath = filePath;
// setting image control source
var strUri = new Uri(WebCamControl.globalFilePath, UriKind.Relative);
previewImage.Source = BitmapFromUri(strUri);
//Method
public static ImageSource BitmapFromUri(Uri source)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = source;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
return bitmap;
}
// delete image
previewImage.Source = null;
if (System.IO.File.Exists(WebCamControl.globalFilePath))
{
System.IO.File.Delete(WebCamControl.globalFilePath);
}
else
{
MessageBox.Show("File Not Exists");
}
After deleting the image in the directory file the image image control should show the new image, but my Image control shows the deleted image. please give me the solution.
In WPF, we generally don't need to use actual BitMapImage objects to display an Image. It's far easier to let the .NET Framework convert our string file paths to the images to the actual Image elements.
Also, it is far better to data bind the Image.Source to a property that implements the INotifyPropertyChanged interface (or a DepenedencyProperty):
<Image Source="{Binding YourImageFilePath}" ... />
Then, whenever a file path of a new image is set to the YourImageFilePath property, your displayed image will update immediately.
YourImageFilePath = filePath;
Try This one:
string path = ((BitmapImage)img.Source).UriSource.LocalPath;
img.SetValue(System.Windows.Controls.Image.SourceProperty, null);
File.Delete(path);
OR
string path = ((BitmapImage)img.Source).UriSource.LocalPath;
img.Source = null;
File.Delete(path)

Adding image to a button in WPF C#, image not appearing

I want to create a button in my windows 8 desktop app, which will have an image and a text block. I have to do this using C# coding.
My code is as follows,
Button btn = new Button();
StackPanel btnContentPanel = new StackPanel();
btnContentPanel.Orientation = Orientation.Horizontal;
Image img = new Image();
img.Source = new BitmapImage(newUri(#"C:\Users\Desktop\Images\download.jpg"));
img.Stretch = Stretch.Uniform;
btnContentPanel.Children.Add(img);
TextBlock txBlock = new TextBlock();
txBlock.Text = "My Button";
btnContentPanel.Children.Add(txBlock);
btn.Content = btnContentPanel;
This is not giving any error but the image is not getting displayed. If I add another text block in place of the image, then its appearing, but not the image.
Am I missing anything ? Please help, thank you.
Try building your button like this:
Button btn= new Button
{
Width = 30,
Height = 30,
Content = new Image
{
Source = new BitmapImage(#"C:\Users\Desktop\Images\download.jpg"))
}
};
In the case of a 'missing' image there are several things to consider:
When Xaml can't locate a resource it might ignore it (when it won't throw a XamlParseException)
The Resource must be properly added and defined:
make sure it exists in your project where expected.
Make sure it is built with your project as a Resource.
(Right click -> Properties -> BuildAction='Resource')
Another thing to try in similar cases, which is also useful for reusing of the image (or any other resource):
Define your Image as a Resource in your Xaml:
<UserCondrol.Resources>
<Image x:Key="MyImage" Source.../>
</UserControl.Resources>
And later use it in your desired control/controls:
<Button Content={StaticResource MyImage} />

Categories

Resources