Add image to DataGridView using DataGridViewImageColumn - c#

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

Related

How to show my datepicker and image from datagridview to datepickerbox and picturebox

This is the code I am facing problem .. in fetching uyearofjoin(datapicker) and picture from picturebox how to write code for it
uname.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
ufather.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
umother.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
usurname.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
uphone.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
uemail.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
ugrno.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
ucourse.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
urollno.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
uyearofjoin.Text = dataGridView1.SelectedRows[0].Cells[11].Value.ToString();
You want to display datetime and Image from dataGridView columns in a DateTimePicker and PictureBox controls respectively.
Let's assume that datetime is available in column 10 of DataGridView and Image is in column 11. The code to populate default value in DateTimePicker will be:
dateTimePicker1.Value = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells[9].Value.ToString());
Code to display image in PictureBox will be:
//Get the blob data in a variable
var data = (Byte[])(dataGridView1.Rows[e.RowIndex].Cells[10].Value);
//Create MemoryStream using blob data of image
var stream = new MemoryStream(data);
//Set the stream data to PictureBox
pictureBox1.Image = Image.FromStream(stream);
// e.RowIndex => selected row for which data is displayed in control
friends how to show image in proper size in stretch ..
..//Get the blob data in a variable
var data = (Byte[])(dataGridView1.Rows[e.RowIndex].Cells[10].Value);
//Create MemoryStream using blob data of image
var stream = new MemoryStream(data);
//Set the stream data to PictureBox
pictureBox1.Image = Image.FromStream(stream);
...image is not showing in proper size it is showing zoom how to resolve this error ....the code is perfect and working only this issuse i want image in proper format size like stretch
can anyone tell me the datepicker value from datagridview is not showing in my datepicker
showing only the default date value (8/10/2013)
and even when updating the datepicker the value is shown in my
datagridvierw (example 10/12/2015) updated but onclick of mouseclick event the value is not showing instead of updated date it is showing the default date value (8/10/2013)again
not showing updated one

how to cast (or convert) an ImageSource to FileImageSource [duplicate]

I am trying add a background image using the image property in button. The issue I'm facing is that i can't set StreamImageSource as button background. I encountered the error given below if I try to do so.
The Code I use to set Image:
ImageSource iconsource =ImageSource.FromStream(() => new MemoryStream(ImgASBytes));
Button Icon = new Button ();
Icon.Image = iconsource ;
The Error I encounter:
Error CS0266: Cannot implicitly convert type 'Xamarin.Forms.ImageSource' to 'Xamarin.Forms.FileImageSource'. An explicit conversion exists (are you missing a cast?)
ImageSource.FromStream () returns a StreamImageSource (see docs). Button.Image accepts only FileImageSource (see docs).
It means that what you're trying to achieve won't work, no matter how hard you try to cast one into the other.
Button.Image will accept images stored as resources in your platform projects, and loaded either with:
Icon.Image = ImageSource.FromFile ("foobar.png");
or
Icon.Image = "foobar.png";
The accepted answer is true that you can't cast StreamImageSource to FileImageSource, I think that the real question is about how to share images in a PCL and use them on a button, just like one would when creating an Image forms control.
The answer is to have a Grid which contains both a Button and an Image object, where the Image overlaps the Button.
For example, the C# code might look like this:
ImageSource imageSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
Button iconButton = new Button ();
iconButton.VerticalOptions = LayoutOptions.FillAndExpand;
iconButton.HorizontalOptions = LayoutOptions.FillAndExpand;
var image = new Image();
image.Source = imageSource;
// So it doesn't eat up clicks that should go to the button:
image.InputTransparent = true;
// Give it a margin so it doesn't extend to the edge of the grid
image.Margin = new Thickness(10);
var grid = new Grid();
// If we don't set a width request, it may stretch horizontally in a stack
grid.WidthRequest = 48;
// Add the button first, so it is under the image...
grid.Children.Add(iconButton);
// ...then add the image
grid.Children.Add(image);
You may have to play with the sizes and thickness values but this should get you a clickable button with an icon.
As of Xamarin.Forms 3.4.0 you can now use ImageButton. You can use embedded images by using an extension method explained in this MS document
Careful with upper- and lowercase in filenames.
I was wondering, why my button-images were shown properly on the simulator, but not on my iPhone.
On the device the filename must match exactly, the simulator doesn't care about upper- and lowercase in filenames.
I use this and it works
var imageA = new Image();
imageA.Source=(FileImageSource)ImageSource.FromFile(allergeneLocation)};
or
var imageA = new Image()
{
BackgroundColor = Color.Teal,
Source = (FileImageSource)ImageSource.FromFile(allergeneLocation)},
};
Here is what I tried:
Button refreshBut = new Button
{
Image = (FileImageSource)
(ImageSource.FromFile("refreshBut.png"))
};
While it compiles I then get an unhandled null reference exception with the description: Object reference not set to an instance of an object. I am not sure if this will help anyone else try to solve this but I am at the same wall.

Add image to cell - iTextSharp

I'm trying to add an image to a pdf cell and I get an error:
Argument 1: cannot convert from 'System.Drawing.Image' to
'iTextSharp.text.pdf.PdfPCell'
in line:
content.AddCell(myImage);
Here's my code:
PdfPTable content = new PdfPTable(new float[] { 500f });
content.AddCell(myImage);
document.Add(content);
myImage variable is of the Image type. What am I doing wrong?
You can't just add an image, you need to create the cell first and add the image to the cell:
http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPCell.html#PdfPCell(com.itextpdf.text.Image)
PdfPCell cell = new PdfPCell(myImage);
content.AddCell(cell);
You also cannot use the System.Drawing.Image class, iTextSharp has it's own class of Image:
http://api.itextpdf.com/itext/com/itextpdf/text/Image.html
It needs a URL passed to the constructor to the image location.
So:
iTextSharp.text.Image myImage = iTextSharp.text.Image.GetInstance("Image location");
PdfPCell cell = new PdfPCell(myImage);
content.AddCell(cell);
You should create a cell first, then add the image to that cell and finally add the cell the the table.
var image = iTextSharp.text.Image.GetInstance(imagepath + "/logo.jpg");
var imageCell = new PdfPCell(image);
content.AddCell(imageCell);
See answer on this post: How to add an image to a table cell in iTextSharp using webmatrix

C# Adding Image to a Label

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

Binding a System.Drawing.Image to a Gridview in ASP.Net

I have defined a gridview control with a column of type ImageField and I bind the gridview to a List<MyRecord>.
MyRecord includes a property of type System.Drawing.Image, but the image is not rendered in the gridview.
There are numerous articles re binding using the URL field which points to (say) a jpg file... but i have the actual Image, not a file.
Any suggestions?
Create a custom ImageHandler like this article describes:
Image Handling In ASP.NET
That's not how the ImageField works. It expects the URL to the image.
<asp:ImageField DataImageUrlField = "ImageUrl" ...
The Handler.ashx worked... thank you
in the code, i set the ImageUrl = string.Format("~/HandlerGetImage.ashx?id={0}", imageNumber);
This dynamically calls the Handler in which i get the image index by
string v = context.Request.QueryString["id"];
Then, the following returns the image:
int ix = Convert.ToInt32(v);
MemoryStream ms = new MemoryStream(Query.imageList[ix]); // imagelist is created elsewhere
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
context.Response.ContentType = "image/jpeg";
//saving bitmap image
returnImage.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
returnImage.Dispose();

Categories

Resources