I have a Windows Form program that will generate HTML code dynamically and a WebBrowser will display the generated code.
Is it the only way, to save a temp HTML file, then display it in thw WebBrowser? Is there a way that I don't need to save and display instantly in the WebBrowser?
Yes. You can use the WebBrowser.DocumentStream property like this:
var myHTMLString = "<html><body><h1>Hello!</h1></body></html>";
// Convert your html string into a byte array
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(myHTMLString);
// Create a MemoryStream from the byte array
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
// Assign the new MemoryStream to the web browser
myWebBrowser.DocumentStream = ms;
Hope it helps!
Related
I'm new to stack overflow, C# and onenote interop com api. I'm trying to display a pdf file in onenote using C# and the onenote com/interop api (I'd rather not use the REST API).
I am able to display a link to a pdf file using the tag < InsertedFile pathSource="[myfilepath]" preferredName = "[myPreferredName]"> in conjunction with the UpdatePageContent function in the interop API, but this doesn't display the PDF.
I have been able to get my program to display an image in onenote using the following code to create the image tag
private XElement createImageTag(Image image)
{
string OneNoteNamespace = "http://schemas.microsoft.com/office/onenote/2013/onenote";
var img = new XElement(XName.Get("Image", OneNoteNamespace));
var data = new XElement(XName.Get("Data", OneNoteNamespace));
data.Value = this.toBase64(image);
img.Add(data);
return img;
}
private string toBase64(Image image)
{
using (var memoryStream = new MemoryStream())
{
image.Save(memoryStream, ImageFormat.Png);
var binary = memoryStream.ToArray();
return Convert.ToBase64String(binary);
}
}
I tried altering this for a pdf instead of am image by converting a pdf to a byte array then converting it to base64 and assigning the result as data.Value in the createImageTag function but it did not result in a displayed pdf either (presumably because onenote was expecting an image and not a pdf). I'd like to avoid using third party libraries or extensions to convert a pdf to an image if possible, and haven't found any other ways to convert a pdf to an image.
I used ONOMSpy to look for any other onenote/xml tags that might help me display a pdf in onenote, but didn't see others besides the Image and InsertedFile tags that looked like they were close to doing what I wanted.
so if you could help me either :
1) find an easy way to convert a pdf to an image using C# or
2) show me how to tell onenote to display the PDF
I'd really appreciate it. Thanks!
Situation:
I am reading a PNG file, encoded with base64 from a server (JSON Format). Works fine.
There is NO direct URL to the ressource (just like: http:... / image.png or simuliar), so i read the data (which is part of a JSON object), decode in from the base64 Encoding and store it in a byte[].
Want i want: Display this PNG on a certain page ( like:
ImageOnPage.Source = myPNG;
)
I cant find a way to make PNG-data to a bitmap. With jpegs i could do something like
using (var stream = new MemoryStream(data, 0, x, true, true)) {
var wbp = new WriteableBitmap(1, 1);
wbp.LoadJpeg(stream);
profileImage.Source = wbp;
}
(sorry, code not testet)
I tried to look around and find the PNG Writer Library - but i still didn't find a way to do something to convert my internal PNG to a useable Bitmap for Setting the Image.Source.
Any help appreciated!
This is what i would like to do:
Get a 64base byte array from database (which is actually in pdf format). This works.
Then i would like to show the pdf in a webbrowser component.
I first started with saving the pdf to a file.pdf and then open it:
byte[] bitjes = isc.GetFileById(fileid); // Getting the bytes
FileStream stream = new FileStream(#"C:\NexusPDF\" + filename, FileMode.CreateNew);
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(bitjes, 0, bitjes.Length);
writer.Close();
webBrowser.Navigate(#"C:\NexusPDF\" + filename);
But that gave me all sorts of problems involving read/write acces. So i figured i have to use the memorystream class to solve this problem.
byte[] bitjes = isc.GetFileById(fileid);
MemoryStream memstream = new MemoryStream(bitjes);
BinaryWriter writer = new BinaryWriter(memstream);
writer.Write(bitjes, 0, bitjes.Length);
writer.Close();
But here's where i'm stuck! I can't just show this in a webBrowser component can i?
Do i have to use the binaryreader before i can show the pdf?
Am i approaching this problem the right way, or are there better alternatives?
Main thing is that i don't want to save the file on disk.
Any help will be appreciated.
You may be able to use the data URL scheme. This URL scheme specifies the content inline.
webBrowser.Navigate("data:application/pdf;base64," + X);
Where X is the base 64 string.
No need to convert the base 64 PDF string into a byte array!
See http://www.ietf.org/rfc/rfc2397.txt for more details.
My C# program needs to display, of many possible images, one at a time. The images are on the web and I have a precise URL for each one. The program needs to either load the image from the web or, if it has been loaded previously, load it from memory/file (as the previous loading from the web should have saved it to memory/file). How do I implement this? I can get the loading from the web with a WebRequest object, but that's not enough to save it for later, faster, use.
WebRequest request = WebRequest.Create(imageURL);
Stream stream = request.GetResponse().GetResponseStream();
pictureBoxFirstPack.Image = Image.FromStream(stream);
I'm pretty sure you should be able to do this:
MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
Byte[] data = ms.ToArray();
Once you have it as a Byte array you can store it in a dictionary, a database, or wherever you like really.
Use the Image.Save method to save the downloaded image (follow an example from MSDN: http://msdn.microsoft.com/en-us/library/9t4syfhh.aspx)
// Construct a bitmap from the button image resource.
Bitmap bmp1 = new Bitmap(typeof(Button), "Button.bmp");
// Save the image as a GIF.
bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Gif);
One of the possible ways to check if the image already exists in your local storage is to calculate the hash-sum of every downloaded image and save it inside a dictionary
SHA256Managed sha = new SHA256Managed();
byte[] checksum = sha.ComputeHash(stream);
var hash = BitConverter.ToString(checksum).Replace("-", String.Empty);
// Store hash into a dictionary
You can use the WebClient to download file like this:
string fileNameLocally = #"c:\file.jpg";
using(WebClient client = new WebClient())
{
client.DownloadFile(imageURL, fileNameLocally);
//you can also use DownloadAsyncFile this methods do not block the calling thread.
}
I want to export one gridview table to excel format.
The most simple and fast forward solution that I found is from Math Berseth
http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html
This solution works fine and was accepted by client. But now, after some months, a new feature was requested: "Just put one image logo in excel"
This is freak me out. I can't put the System.Drawing.Image in a System.Web.UI.WebControls.Image cause they are completely different, but I'm not able to just put a Path cause the excel generated will be send in e-mails so Directory structure can't be considered.
So, can I put images retrieved from bytes in Gridview to export in Math model, or exist some other way?
edit..
I walk few more steps but I'm still far away from my goal.
I can embedded images in html files using String Base64
Something like:
private string MakeImageSrcData(string filename) {
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
byte[] filebytes = new byte[fs.Length];
fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
return "data:image/png;base64," + Convert.ToBase64String(filebytes, Base64FormattingOptions.None);
}
...
string base64 = MakeImageSrcData("D:\\Proj\\top_title.png");
TableRow tr = new TableRow();
TableCell tc = new TableCell();
Image logoEmpresa = new Image();
logoEmpresa.ImageUrl = base64;
tc.Controls.Add(logoEmpresa);
tr.Cells.Add(tc);
table.Rows.Add(tr);
This works fine with IE and FF but nothing whit excel :/
I tried spreadsheet xml, but as MSDN describes here there are no support to image type.
Some other idea?
This method works by outputting the gridview as text containing an HTML table. I would imagine you could prepend the string with a string containing an <img> tag pointing to your logo somewhere on an accessible web site.
Give it a try.
Comment
I added the following before line 61 in the GridViewExportUtil.cs file in the referenced demo:
HttpContext.Current.Response.Write("<img src='http://localhost/WebApplication2/wand.gif' />");
The image was available at the specified URL, and rendered correctly in Excel.