I have a requirement to be be able to embed scanned tiff images into some SSRS reports.
When I design a report in VS2005 and add an image control the tiff image displays perfectly however when I build it. I get the warning :
Warning 2 [rsInvalidMIMEType] The value of the MIMEType property for the image ‘image1’ is “image/tiff”, which is not a valid MIMEType. c:\SSRSStuff\TestReport.rdl 0 0
and instead of an image I get the little red x.
Has anybody overcome this issue?
Assuming you're delivering the image file via IIS, use an ASP.NET page to change image formats and mime type to something that you can use.
Response.ContentType = "image/png";
Response.Clear();
using (Bitmap bmp = new Bitmap(tifFilepath))
bmp.Save(Response.OutputStream, ImageFormat.Png);
Response.End();
I have been goggling fora solution on how to display a TIFF image in a SSRS report but I couldn't find any and since SSRS doesn's support TIFF, I thought converting the TIFF to one of the suppported format will do the trick. And it did. I don't know if there are similar implementation like this out there, but I am just posting so others could benefit as well.
Note this only applies if you have a TIFF image saved on database.
Public Shared Function ToImage(ByVal imageBytes As Byte()) As Byte()
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(imageBytes)
Dim os As System.IO.MemoryStream = New System.IO.MemoryStream()
Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(ms)
img.Save(os, System.Drawing.Imaging.ImageFormat.Jpeg)
Return os.ToArray()
End Function
Here’s how you can use the code:
1. In the Report Properties, Select Refereneces, click add and browse System.Drawing, Version=2.0.0.0
2. Select the Code Property, Copy paste the function above
3. Click Ok
4. Drop an Image control from the toolbox
4.1. Right-Click the image and select Image Properties
4.2. Set the Image Source to Database
4.3. In the Use this field, Click expression and paste the code below
=Code.ToImage(Fields!FormImage.Value)
4.4. Set the appropriate Mime to Jpeg
Regards,
Fulbert
Thanks Peter your code didn't compile but the idea was sound.
Here is my attempt that works for me.
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "image/jpeg";
Response.Clear();
Bitmap bmp = new Bitmap(tifFileLocation);
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
Response.End();
}
Related
I have a database program that stores images to the SQL DB and reads them back to be displayed in a WPF application. if i use Jpeg images it works fine, but if i use PNG images, which i wanted to use to try and keep the transparency ( which disappears anyway when stored ) most of the images come back corrupt.
this is the image that has been selected
i then save it to the db, and add the image the listview
then if i close the application and reload it, it pulls the image back from the db, you can see that it is corrupt in the listview
and then when i select it, the image control also shows the corrupted image
i am storing the image in code using a BitmapImage object, and use this to set the image.source, and also convert this to a byte[] for storing into the image field in the database.
i convert the bitmapimage to a Byte[] with the following line
command.Parameters.AddWithValue("#Image", ImageToByteArray(productImage.ProductImage));
and these are the functions to convert to and from a bitmapimage
private static BitmapImage BuildImage(byte[] image)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
MemoryStream mem = new MemoryStream(image);
bitmap.StreamSource = mem;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
//bitmap.Freeze();
return bitmap;
}
private static byte[] ImageToByteArray(BitmapImage image)
{
byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
return data;
}
works fine with Jpegs, but then i have the white background to the images.
any help would be much appreciated.
"JpegBitmapEncoder" is a large clue. Surely you'd want "PNGBitmapEncoder"...?
As personal preference I would change your SQL data column to varbinary(MAX) and use stream and BinaryReader objects to upload the file.
However I think your issue is you are not using PngBitmapEncoder for the PNG you are using the JpegBitmapEncoder irrespective of filetype.
Hope this helps.
For a while now I have noticed whenever I export reports from the ReportViewer control (Webforms version) to PDF format, any included images lose quality and appear slightly pixelated.
They look just fine in the ReportViewer however.
From what I have read, the PDF renderer will size any included images at 96 dpi, no matter what dpi the image is originally.
I have done some digging and came across this post here
I have tried this approach in my own code behind by wiring up a button like so
protected void btnExport_Click(object sender, EventArgs e)
{
string mimeType, encoding, fileNameExtension;
Warning[] warnings;
string[] streams;
var sb = new StringBuilder(1024);
var xr = XmlWriter.Create(sb);
xr.WriteStartElement("DeviceInfo");
xr.WriteElementString("DpiX", "300");
xr.WriteElementString("DpiY", "300");
xr.Close();
byte[] bytes = ReportViewer1.ServerReport.Render("PDF", sb.ToString(), out mimeType, out encoding,
out fileNameExtension, out streams, out warnings);
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=Test.pdf");
Response.BinaryWrite(bytes);
}
This actually makes my images appear much smaller in the exported PDF compared to what shows in the Report Viewer control.
My original images are 600x600 at 300dpi, I have tried using these images as they are and on the image properties in the report RDL designer set the sizing property to 'Fit' and sizing the image to 0.25in x 0.25in. Again, all looking great in preview mode in the Report Viewer control but then quality is lost when exporting to PDF.
I tried resizing the images to 0.25in x 0.25in in my image editor (paint.net) leaving at 300 dpi, but still no difference in the results.
I'm just going round in circles now, no doubt I am missing something. I hope there is a way and someone can shed some light for me?
Thanks!
I have a requirement in which I have to generate a pdf and then on click of button "SHOW PDF", I have to display on another window.
I have been able to generate a pdf using IText and stored in my machine. I get a java.io.File object as my return value from my backend library which needs to be displayed on the screen. Can someone please guide me how to do this?
My xhtml file has the following code snippet:
<h:commandLink action="PdfDisplayRedirect.xhtml" target="_blank">show PDF</h:commandLink>
my PdfDisplayRedirect.xhtml has the following code:
<p:media value="#{pdfGenerationAction.fileName}" width="100%" height="300px">
Your browser can't display pdf, <h:outputLink value="InitialExamination33.pdf">click</h:outputLink> to download pdf instead.
My backing bean has the following code:
private File initialExaminationFile;
private generateFile(){
this.initialExaminationFile = backendService.generateFile();
}
On clicking, I get a new window opened but the pdf file is not displayed.. Instead my screen from where I had invoked the command gets displayed there.
Any help would be really appreciated.
Thanks
Thanks for the response and no response.
I have found a solution myself which I would like to post so that those looking for a solution can use it.
My xhtml file included a commandlink
<p:commandLink actionListener="#{pdfGenerationAction.generatePDF(initialExaminationEMRAction.patientID)}" oncomplete="window.open('PdfDisplayRedirect.xhtml')">broadcast Msg</p:commandLink>
My pdfGenerationAction bean file had the following lines of code:
FileInputStream fis = new FileInputStream(this.initialExaminationFile);
//System.out.println(file.exists() + "!!");
//InputStream in = resource.openStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
//Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
System.out.println("read " + readNum + " bytes,");
}
this.reportBytes = buf;
}
I converted my file into bytearraystream and made it available in my session. Then I followed the suggestion given by BalusC at Unable to show PDF in p:media generated from streamed content in Primefaces
How I can create PDF files from a list of image in WinRT. I found something similar for windows phone 8 here("Converting list of images to pdf in windows phone 8") But I am looking for a solution for windows 8. If anyone of having knowledge about this please share your thoughts with me.
Try http://jspdf.com/
This should work on WinJS, but I haven't tested it. In a XAML app you can try to host a web browser control with a jsPDF-enabled page.
ComponentOne has now released the same PDF library that they had in Windows Phone for Windows Runtime. Tho it's not open source, of course.
Amyuni PDF Creator for WinRT (a commercial library) could be used for this task. You can create a new PDF file by creating a new instance of the class AmyuniPDFCreator.IacDocument, then add new pages with the method AddPage, and add pictures to each page by using the method IacPage.CreateObject.
The code in C# for adding a picture to a page will look like this:
public IacDocument CreatePDFFromImage()
{
IacDocument pdfDoc = new IacDocument();
// Set the license key
pdfDoc.SetLicenseKey("Amyuni Tech.", "07EFCD0...C4FB9CFD");
IacPage targetPage = pdfDoc.GetPage(1); // A new document will always be created with an empty page
// Adding a picture to the current page
using (Stream imgStrm = await Windows.ApplicationModel.Package.Current.InstalledLocation.OpenStreamForReadAsync("pdfcreatorwinrt.png"))
{
IacObject oPic = page.CreateObject(IacObjectType.acObjectTypePicture, "MyPngPicture");
BinaryReader binReader = new BinaryReader(imgStrm);
byte[] imgData = binReader.ReadBytes((int)imgStrm.Length);
// The "ImageFileData" attribute has not been added yet to the online documentation
oPic.AttributeByName("ImageFileData").Value = imgData;
oPic.Coordinates = new Rect(100, 2000, 1200, 2000);
}
return pdfDoc;
}
Disclaimer: I currently work as a developer of the library
For an "open source" alternative it might be better for you to rely on a web service that creates the PDF file using one of the many open source libraries available.
I think this may help you if you want to convert an image (.jpg) file to a PDF file.
Its working in my lab.
string source = "image.jpg";
string destinaton = "image.pdf";
PdfDocument doc = new PdfDocument();
doc.Pages.Add(new PdfPage());
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
XImage img = XImage.FromFile(source);
xgr.DrawImage(img, 0, 0);
doc.Save(destinaton);
doc.Close();
Thanks.
My app retrieves file content from remote server. File can be image or text. When the file is image it returns some string like this:
????\bNExif\0\0MM\0*\0\0\0\b\0\a\0.... and so on. As I understand it's an image but in other format (binary?).
So how can I convert that string to an image and set it to control as source?
Thanks.
I think you can do something like this:
MemoryStream ms = new MemoryStream(bytes);
BitmapImage bi = new BitmapImage();
bi.SetSource(ms);
Then if you have an Image element in XAML, say XamlImage:
XamlImage.Source = bi;