- Home /
How to convert to XImage without using System.Drawing.Image with the webplayer
Hi, I am having problems creating a PDF Screenshot with text data. What I am planing on doing is creating a screenshot and printing it into a PDF File, which is opened in a new window. My Problems are: Changing the image file from Bytes[] -> Stream -> XImage. Calling the new PDF in a new Tab / Window.
Ive got all this working fine on the Stand alone Server, but not on the Web Player version. Is there any way that I can use parts of system.drawing.image? or to save the files in temp_internet_files or something like that.
I can convert the Screenshot to a PNG or Stream, but not to an Image file, which I need for the PDF. I am using PDFSmart to create the PDF (are there any other PDF Creators with which I can easily add Image files without converting them?). Then I need to open the PDF in a new browser tab. I have seen some examples with ASP pages which I dont have and I can't get RESPONSE to work either, I guess System.Web is somehow not working either or different.
Here is my code:
using UnityEngine;
using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Configuration;
using System.Web;
// using System.Web.UI; // using System.Web.UI.WebControls; // using System.Web.UI.WebControls.WebParts; // using System.Web.UI.HtmlControls;
using System.Net; using PdfSharp; using PdfSharp.Drawing; using PdfSharp.Pdf; using PdfSharp.Pdf.IO; using PdfSharp.Drawing.Layout; using PdfSharp.Pdf.Advanced;
public class ScreenshotToPdf : MonoBehaviour {
void pdfDrawHeader(Texture2D pngTexture){
MemoryStream ms = new MemoryStream(pngTexture.EncodeToPNG());
Bitmap bmp = (Bitmap)Image.FromStream(ms, true, true);
XImage image = XImage.FromGdiPlusImage(bmp);
PdfDocument document = new PdfDocument();
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
//..Some PDF Creation
double factor = 1.0f;
double width = image.PixelWidth;
double height = image.PixelHeight;
if(width > 480.0f){
factor = width / 480.0f;
}
if(height > 360.0f && (height / 360.0f)>factor){
factor = height / 360.0f;
}
if(factor==0.0f)
factor=1.0f;
width /= factor;
height /= factor;
gfx.DrawImage(image, (pdfwidth-hspace*2)*0.5-(width*0.5),vspace,width,height);
imageHeight = vspace+height;
MemoryStream stream = new MemoryStream();
document.Save(stream, false);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", stream.Length.ToString());
Response.BinaryWrite(stream.ToArray());
Response.Flush();
stream.Close();
Response.End();
}
}
I could load the image file to the server and read it againg from there, but I would have the problem that 'XImage image = XImage.FromFile' only opens local files.
Does anyone have an idea how I could solve this problem?
Your answer
Follow this Question
Related Questions
Alternative to 'Application.OpenURL()' for newest Android API 1 Answer
How to save runtime generated texture in Webplayer to hard drive? 1 Answer
Can't replace loading image in web player 1 Answer
Custom background image in Unity WebPlayer Loading Screen 0 Answers
WbPlayer Loading From File 0 Answers