Decoding base64 Stream to image - c#

I am sending base64 encoded image from client side using javascript (I am creating Screenshot uploader applet for asp.net application using http://supa.sourceforge.net/) and this sends an ajax request to server to store the image. At server I am using HttpContext in GenericHanlder in asp.net application.
How to convert image data from HttpContext to image at server?

First, you need to convert the base 64 back into bytes:
byte[] data = System.Convert.FromBase64String(fromBase64);
Then, you can load it into an instance of Image:
MemoryStream ms = new MemoryStream(data);
Image img = Image.FromStream(ms);
If you want to save it to a file instead, use System.IO.File.WriteAllBytes

I needed to do something similar, but wanted to work directly with the InputStream, so used this to do the decoding:
// using System.Security.Cryptography
var stream = new CryptoStream(Request.InputStream, new FromBase64Transform(), CryptoStreamMode.Read);
var img = Image.FromStream(stream);

Related

Audio file is not working via FTP upload programatically

I am uploading an .mp3 file via FTP code using C#, the file is uploaded successfully on server but when i bind to a simple audio control or directly view in browser it does not work as expected, whereas when i upload manually on the server it works perfectly.
Code:
var inputStream = FileUpload1.PostedFile.InputStream;
byte[] fileBytes = new byte[inputStream.Length];
inputStream.Read(fileBytes, 0, fileBytes.Length);
Note: When i view the file in Firefox it shows MIME type is not supported.
Thanks!
You're reading the file as a string then using UTF8 encoding to turn it into bytes. If you do that, and the file contains any binary sequence that doesn't code to a valid UTF8 value, parts of the data stream will simply get discarded.
Instead, read it directly as bytes. Don't bother with the StreamReader. Call the Read() method on the underlying stream. Example:
var inputStream = FileUpload1.PostedFile.InputStream
byte[] fileBytes = new byte[inputStream.Length];
inputStream.Read(fileBytes, 0, fileStream.Length);

convert datauri pdf to file for attaching to email

I am using a basic telerik export document to pdf function. this works great to export the page directly to the user. I then pass this to a controller as a string via datauri.
how can I convert it back to a file so that I can attach it to an email?
imageData: "data:application/pdf;base64,JVBERi0xLjQKJcLB2s/OCgoxIDAg...
I found a way to do this. replace a bit of the header, convert to byes, make a new stream, attach stream to email.
imageData = imageData.Replace("data:application/pdf;base64,", "");
byte[] bytes = Convert.FromBase64String(imageData);
Stream stream = new MemoryStream(bytes);
email.Attachments.AddFileAttachment("SALOR.pdf",stream);

I want to save captured image in an Object (Windows Phone App)

i am working in windows Phone 7 Application. using this code i captured the image and saved into Media Library
myCamera.Show();
and this is for Saving to media Library
mediaLibrary.SavePicture("TestPhoto", imageBits);
My Question is > I want to save my captured image into an Object Where i can directly send to server
imageBits is already an object (of type Stream) so what you're asking for doesn't really make sense. Presumably you're trying to convert it to a byte array in order to send it to the server.
MemoryStream ms = new MemoryStream();
//if you've manipulated stream before this call, reset position
e.ChosenPhoto.Position = 0;
e.ChosenPhoto.CopyTo(ms);
byte[] imageByteArray = ms.ToArray();
ms.Dispose();
imageByteArray then contains your image as a byte array. Alternatively, you could convert the image into a Base64 encoded string and send that, but that depends if your server can decode it.
string base64 = Convert.ToBase64String(imageByteArray);

How to store an image in a SQL Server database from Silverlight app?

I am trying to store some pictures into my SQL Server database from a Silverlight project, and I need some help, so my questions are:
How to convert an image to binary from a url to store it into my database (store all the image and not only the url)
Are there any other solutions, without passing by binary type? (since it exist the image type in SQL Server)
Finally, when the image is stored, how to read it from Silverlight?
Thank you in advance .
You'll want to convert the System.Drawing.Image to a byte array and save the byte array to the database.
System.Drawing.Image image;
System.IO.MemoryStream imageStream;
byte[] imageBytes;
// image = your image object
imageStream = new System.IO.MemoryStream();
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg); // Use whatever format your image is.
imageBytes = imageStream.ToArray();
// Save imageBytes to a DB column of type VARBINARY(MAX)
To get the data back into an System.Drawing.Image object from a byte array use System.Drawing.Image.FromStream(System.IO.Stream stream).
Download file contents and put into memory stream. See : http://www.csharp-examples.net/download-files/
Add bytes from memory stream into database
Get image with silverlight in C# by using the image class and method FromStream see http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx
http://www.pitorque.de/MisterGoodcat/post/Storing-images-in-SQL-Server-with-RIA-Services.aspx
It is fully functional as in the user can select images from the local disk and upload them to the service where they are stored in the database. The user can retrieve a list of all images in the database and download them to the client for watching, and they can delete existing images from the database.

how to convert a jpeg in an array into a bitmap

I have a functioning application in c#/.net that currently accepts raw image data in a bayer format from a set of embedded cameras and converts them to jpeg images. To save transmission time, I have modified the embedded devices to encode the images as jpegs prior to transmission. I'm an experienced embedded programmer but a total c#/.net noob. I have managed to modify the application to save the arrays to file with a jpeg name using this snippet: ( the offset of 5 is to skip header data in the transmission frame)
FileStream stream = File.Create(fileName);
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(multiBuff.msgData, 5, multiBuff.dataSize - 5);
writer.Close();
The files open up fine, but now I want to treat the data as a bitmap without having to save & load from file. I tried the following on the data array:
MemoryStream stream = new MemoryStream(data);
BinaryReader reader = new BinaryReader(stream);
byte[] headerData = reader.ReadBytes(5);
Bitmap bmpImage = new Bitmap(stream);
But this throws a parameter not valid exception. As a newbie, I'm a little overwhelmed with all the classes and methods for images and it seems like what I'm doing should be commonplace, but I can't find any examples in the usual places. Any ideas?
I think you are looking for Bitmap.FromStream() :
Bitmap bmpImage = (Bitmap)Bitmap.FromStream(stream);
Actually using new Bitmap(stream) should have worked as well - this means that the data in the stream does not constitute a valid image - are you sure the jpg is valid? Can you save it to disk and open it i.e. in Paint to test?
You use the Image class.
Image image;
using (MemoryStream stream = new MemoryStream(data))
{
image = Image.FromStream(stream);
}
FYI it didn't work because reader.ReadBytes(5) returns the 5 first bytes of stream not the bytes after position 5

Categories