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
Related
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
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
I'm generating a PDF file based on a record selected in my datagridview. It will consist of 3-5 pages. I created a table with 2 columns to represent my header. the first cell is left aligned and the 2nd cell is right aligned. I want this same inforamtion displayed on all pages.
After doing some googling, I saw a header.WriteSelectedRows() property which is supposed to help with that? One example was :
header.WriteSelectedRows(0, -1, doc.PageSize.GetLeft(5), doc.PageSize.GetTop(5), wri.DirectContent);
2nd was:
header.WriteSelectedRows(0, -1, doc.LeftMargin, doc.PageSize.Height - 36, wri.DirectContent);
However both resulted in just the first page having the table/header. Any ideas on what I need to fix? Thanks!
Code:
PdfPTable header = new PdfPTable(2);
header.HorizontalAlignment = Element.ALIGN_LEFT;
header.TotalWidth = doc.PageSize.Width - 20f;
header.LockedWidth = true;
Phrase cell1 = new Phrase(signal.ProformaType);
Phrase cell2 = new Phrase("text" + Environment.NewLine + "text"
+ Environment.NewLine + signal.Signal);
PdfPCell c1 = new PdfPCell(cell1);
c1.Border = iTextSharp.text.Rectangle.NO_BORDER;
c1.VerticalAlignment = iTextSharp.text.Element.ALIGN_TOP;
c1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
header.AddCell(c1);
PdfPCell c2 = new PdfPCell(cell2);
c2.Border = iTextSharp.text.Rectangle.NO_BORDER;
c2.VerticalAlignment = iTextSharp.text.Element.ALIGN_TOP;
c2.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT;
header.AddCell(c2);
header.WriteSelectedRows(0, -1, doc.LeftMargin, doc.PageSize.Height - 36, wri.DirectContent);
The PdfPTable is added to the first page only because you are adding it to the first page only. If you want to add it to every page that is created by iText, you shouldn't add the PdfPTable where you are adding it now.
Instead you should add it in the OnEndPage() method of a page event. This is explained in answers to questions such as:
How can I add Header and footer in pdf using iText in java?
how to add an image to my header in iText generated PDF?
How to handle the case in wich an iText\iTextSharp table is splitted in two pages?
...
In other words, you need to create your own implementation of the PdfPageEvent interface. The best way is to extend the PdfPageEventHelper class:
public class MyPageHeader : PdfPageEventHelper
{
PdfPTable header = ... // define header table here
public override void OnEndPage(PdfWriter writer, Document document)
{
header.WriteSelectedRows(0, -1, document.Left, document.Top, writer.DirectContent);
}
}
To make this work, you need to declare this page event before opening the Document:
PdfWriter pdfWriter = PdfWriter.GetInstance(document, pdfFileStream);
pdfWriter.PageEvent = new MyPageHeader();
document.Open();
Now, every time a new page is created, the header will be added automatically.
You may want to adapt document.Left and document.Top in the code above, because right now, it will add the table in the upper-right corner of each page, you may want to use document.Left + 36 and document.Top - 5 or something like that.
Also: make sure that there is sufficient room for the header, otherwise your header will overlap with the content you are adding straight to the Document using document.Add(). You can change the margins in the constructor of the Document class.
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
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();