I have a blob field in the database which stores image blob. I want to convert this to an image in the html image src tag. How can this be done, i am using ASP.NET MVC c# as my backend.
One way is to convert it to Base64String. First convert it to ByteArray then you can get Base64String.
for example:
var imgUrl = $"data:{imageType};base64,{Convert.ToBase64String(buffer)}";
Related
When I send a base64 string to an web api, do I need to pass the mime type e.g. jpg to define the file extension when the file is saved? if not how would I know what file type to save it as?
You could either send the file type in a separate variable, prepend it with a separator to the base64 string then extract it, or convert the base64 string to a byte array and then read the first few bytes to determine the image format.
I have images stored in pdf, jpg , etc. in a varbinary field on an MSSQL server. I can extract them into a byte array, and convert them into a Base64 encoded string in a VB.NET API.
I need to display them on a web page for use in another application. Can I:
a) using httpresponse display them directly from the API? By setting the media type to the appropriate image type?
b) Currently most of the controllers in the solution inherit from the APIController which doesn't appear to allow access to ViewBag, etc. But if I change the controller to inherit from Controller, I am unable to access the method. I get a 404 error or URI doesn't exist error. This solution is easy in C# by setting the data in ViewBag to the Base64 encoded string that displays in a view.
c) access the API from an .aspx page from another web vb.net solution using jquery to parse the parameters passed in the query string.
I am not sure what approach is do-able. It seems like this should be a relatively easy thing to do.
C# Example
Dim documentImageByteArray = documentTable.Rows(0).Item("DocumentData")
Dim Image = ("data:image;base64," +
Convert.ToBase64String(documentImageByteArray))
ViewBag.ImageData = Image
Return View()`
if it's a web API controller then option a would be sensible. Just set the src of the img tag to the URL of the API which fetches the image data (make sure it accepts GET requests). You don't need to convert to base64, just return the binary data with correct headers. –
ADyson
Feb 15, 2019 at 20:46
I have a pdf file in a base64 string format. I need to convert it to an image (any type) to use further in my code.
I have searched SO as well as the web and I have not been successful with anything.
Does anyone have a tried and true method for this?
I can use a third party. I just need a working one!!
Thank you
I used the Apitron PDf Rasterizer
to convert my file. It is very simple to use and works great. No missing dlls and clear documentation. However, you do need to purchase it.
Thanks to all for assistance
base64 is the form of string web friendly representation of byte array. you may convert it to a byte array like this:
byte [] decodedBytes = Convert.FromBase64String (encodedText);
then you may simple save it to a file:
System.IO.File.WriteAllBytes("file.pdf", decodedBytes);
and then finally convert pdf to image
I have an ImageButton in my website that has a dynamic source well it basically looks like this: "data:image/svg+xml;base64,...."
So I am trying to insert an image in to PDF using that. This is the code I use
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(new Uri(((ImageButton) FindControl(fieldKey)).ImageUrl));
I either get a "The URI is empty" error or a path not found.
Any ideas how to approach this?
I doubt anyone would google this but I figured it out so why not post an anwser.
To implement data img types in to PDF remove the prefix part and then convert it back from base 64 in to array byte.
string theSource = ((ImageButton)FindControl(fieldKey)).ImageUrl.Replace("data:image/png;base64,", "");
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(Convert.FromBase64String(theSource));
I've created an application in c# where an image is inserted into xml by turning it into bytes. How do i then convert this image from the xml into a word document?
This article might help you:
Inserting images into Word documents using XML
The main idea of the article is to construct a WordML (WordProcessingML) document fragment representing the image to be inserted and then calling Word's InsertXML function to place the image in the document.