I am currently developing a web application that receives data from an on-site database. The database developers have developed some web-services that I am able to call and send/receive data to and from the the database.
When I want to display an image the method returns the image in BLOB format. My question is: what is the best way to convert BLOB to .jpg or .bmp so I can display the image correctly? If someone could point me in the right direction that would be great!
Cheers,
Tristan
You can create a generic handler (ashx file) and have it return the byte array.
For example, the url http://www.example.com/GetImage.ashx?imageId=1 could contain code to write the bytes received from the image with id of 1.
You will need to implement the handler code yourself to do the database query, etc. and write the bytes out to the response. You will also need to set the response mime type to jpg, png or whatever format your images are in.
Check out this stackoverflow post for more details: How to bind a MemoryStream to asp:image control?
byte[] array and mimetype are the key words :)
There is no one image format associated with BLOB. It's simply a way of putting raw binary in a database. You need to figure out which format it actually is (libmagic may help), then you can convert if needed.
Related
I am storing in the database images, which I get from the frontend through a form. I then convert the file from an IFormFile to an array of bytes, which then gets stored in the database in a varbinary(max) type column.
But when the getEntity endpoint is called, the image gets automatically converted from the array of bytes to a base64 dataURL. Is this behavior normal/is it good practice to send it as a data url and have the frontend decode it to an image?
Also, is it possible to not send an array of bytes, but the whole file, just like i get it from the frontend in an IFormFile interface?
Here is a similar question pertaining to image upload best practices on Microsoft msdn.
https://learn.microsoft.com/en-us/answers/questions/682240/best-practice-for-saving-image-in-database.html
https://social.msdn.microsoft.com/Forums/en-US/5a31c367-e54e-4cb5-924f-42ef40724ff9/user-uploaded-images-best-practices?forum=aspwebforms
when I create a notification in uwp app, and I try set the image, it does work when I do something like:
((XmlElement)imageAttribute[0]).SetAttribute("src", "ms-appx:///Assets/Test.png");
This works fine. But what I need is to set the image from base64 string and not from the Assets folder. Does anyone have any solutions?
You cannot read a string and have it work as binary data. You need to first read the base64 string and convert it back to binary which would be usually in a type of byte array or something.
after you read the base64 string and convert it back to the binary data, then you can use that instead of the binary file in your attribute instead of referencing a resource.
There are multiple sources out there for converting base64 to binary data and/or files so an internet search should yield the results you are looking for... without knowing anything about the language you are writing in, it is impossible to give examples here but the method is the same.
So iv come across this question time and time again in this forum, yet none of them cater for what i need.
I'm using a code first approach, c# mvc5
I'v managed to store a pdf to my database using a data type byte array.
I store pdf's to the database, the exact same way you would save a jpeg. I use file stream.
My question is, how do i now display this pdf that has been stored. I'm completely lost...help please
So you already know how to make the file stream, just return a FileResult instead of a View and the client's browser will know how to display a PDF as seen here.
return File(stream, "application/pdf", "DownloadName.pdf");
I have the following textual binary representation: "0x255044462D312E340D0A25FFFFFFF..."
I know it's a pdf.
I know it's the textual represantation from a sql server column (image data type).
But im lost to find out how to save this binary to a pdf file on my disk and view the content.
Maybe someone can hint me in the right direction.
Best Regards and Thanks in Advance
You're correct that it is a PDF file (at least it masquerades like on. You have hexadecimally encoded bytes; the first read:
255044462D312E340D0A
%PDF-1.4<CR><LF>
So you appear to have a PDF 1.4 string.
Just take two characters from the string, treat them as hex, convert them to the correct byte and write them to a file. Write binary, not textually (you don't want to add additional line-breaks in there, PDF is too binary to let that work.
(I did the conversion using this site: http://www.dolcevie.com/js/converter.html)
I'm not sure what database you are working with or how you are getting your string that you have above.
Many databases allow you to save binary data as a blob or some other byte array type. I believe in MSSQL this is called an "image" but I am not 100% on that. I would start by looking into the two following links in order. The first link talks about how to pull byte array data from a database. The example is in Visual Basic but should be easily changed to C# if that is what you are using.
The second link contains an example of how to save that byte array data to the file system.
I would also suggest posting some of the code you have tried as well so that the community may comment and point out areas you possibly had misunderstandings on.
1.) http://support.microsoft.com/kb/308042
2.) Save and load MemoryStream to/from a file
http://www.pdfsharp.com/PDFsharp/ can read in binary data and you can call .Save() and it will make the PDF file to disk for you.
I need to store XML data on a server that only accepts jpeg images. I thought of writing my XML data inside a valid jpeg file. After all, other than the jpeg header, the content of the image file is arbitrary data right?
Is it possible to produce a valid jpeg file, but have its "body" filled with custom bytes?
Of course, I also need to be able to decode the custom jpeg file and restore the data.
I'm not familiar with the jpeg file format, so I'd appreciate an explicit example.
Perhaps just appending the data to a small jpeg will work?
Create a small jpeg.
Append your (obfuscated/encrypted) XML to the file.
Upload to server.
FWIW, you can easily see this works using a Hex editor. Just create a small jpeg and append your xml to the end. Then open it using any image editor.
This is a perfectly valid thing to do to a jpeg file:
Will random data appended to a JPG make it unusable?
Uhmmm it is a strange architecture... but anyway I think this post would be useful:
How to Add 'Comments' to a JPEG File Using C#
so the proposal is add the data as a metadata of a jpeg blank image.
If you want to add your data as the actually jpeg data, you first create a BitmapSource with BitmapSource.Create and put your data in the buffer parameter. Than use the JpegBitmapEncoder to save it as a jpeg file (an example is here).
However, as far as I know, the .Net jpeg encoder is not lossless (even if you set it's quality to 100%) so you will need a third party library that can encode JPEG lossless.
I don't know of a JPEG specific way but there is a PNG/GIF method to encode arbitrary data and pixels. Check out this post. Some sites allow you to upload PNGs and GIFs renamed to JPEG so you could try that.
http://blog.nihilogic.dk/2008/05/compression-using-canvas-and-png.html
He's saving javascript but you could use and text, really.