I am making a small application in android that browse image from the gallery or take a picture from the camera. Then the selected image is compressed and uploaded to the server. I had compressed the image using Base64 String in android and for uploading image i am making a web service in ASP.NET. But i'm not sure how to decode the string(converted using Base64 in android) into image(the web service should be able to convert it). Please help me.
Thanks in advance
You can convert base64string to image by Image.FromStream. You will need to convert the base64string to stream first.
byte[] imageBytes = Convert.FromBase64String(imgBase64String);
Image img = null;
using (MemoryStream ms1 = new MemoryStream(imageBytes))
{
img = Image.FromStream(ms1);
}
if (img != null)
{
// ...
}
Related
I am binding an image from a byte array in MVC4 view , The byte array is returned from a WEB API which I need to bind it to a view .
Below is the code
string str="JVBERi0xLjUNJeLjz9MNCjQgMCBvYmoNPDwvRSA0NDc1L0ggWyA3NjMgMTI1IF0vTCA1MTczL0xpbmVhcml6ZWQgMS9OIDEvTyA2L1QgNDcwMj4+DWVuZG9iag0gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIA0xNCAwIG9iag08PC9EZWNvZGVQYXJtcyA8PC9Db2x1bW5zIDQvUHJlZGljdG9yIDEyPj4vRmlsdGVyIC9GbGF0ZURlY29kZS9JRCBbKG5cMzY0XGZcMjM2XDMyNVwyNjJ9XDI2NFwyNzBcMzUwKlwxNzdcMDM3XDM2N1wwMDdcMzE2KSAoblwzNjRcZlwyMzZcMzI1XDI2Mn1cMjY0XDI3MFwzNTAqXDE3N1wwMzdcMzY3XDAwN1wzMTYpXS9JbmRleCBbNCAxMV0vSW5mbyAzIDAgUi9MZW5ndGggNTQvUHJldiA0NzAzL1Jvb3QgNSAwIFIvU2l6ZSAxNS9UeXBlIC9YUmVmL1cgWzEgMiAxXT4+DXN0cmVhbQp4nGNiZBBgYGJg2gAkGHeAiLlAgiEHSPAUMTAxfhADcRkYmf4zffrPxCCQAuS+C2cAALmiCM4KZW5kc3RyZWFtDWVuZG9iag1zdGFydHhyZWYNIDANJSVFT0YNDQ0N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ZWFtDWVuZG9iag0xIDAgb2JqDTw8L0ZpbHRlciAvRmxhdGVEZWNvZGUvRmlyc3QgOS9MZW5ndGggMTM0L04gMi9UeXBlIC9PYmpTdG0+Pg1zdHJlYW0KeJwzUjBQMFYwNlewsdF3zi/NK1Ew1PfOTClWiDYDygTF6odUFqQq6AckpqcW29mBFBWlJpZk5ue5JJakKmi4WBkZGJoamBgaGxkYmRqYRhkYqAORpr5vfgoBFQFF+SmlyalFChoBLm4hRfl5CkDaL7VERyHMTM/YwMjEKCZP084OAIrZKrEKZW5kc3RyZWFtDWVuZG9iag0xMyAwIG9iag08PC9EZWNvZGVQYXJtcyA8PC9Db2x1bW5zIDQvUHJlZGljdG9yIDEyPj4vRmlsdGVyIC9GbGF0ZURlY29kZS9JRCBbKG5cMzY0XGZcMjM2XDMyNVwyNjJ9XDI2NFwyNzBcMzUwKlwxNzdcMDM3XDM2N1wwMDdcMzE2KSAoblwzNjRcZlwyMzZcMzI1XDI2Mn1cMjY0XDI3MFwzNTAqXDE3N1wwMzdcMzY3XDAwN1wzMTYpXS9JbmRleCBbMCA0XS9JbmZvIDMgMCBSL0xlbmd0aCAyNC9Sb290IDUgMCBSL1NpemUgNC9UeXBlIC9YUmVmL1cgWzEgMiAxXT4+DXN0cmVhbQp4nGNiAAImRsFqIPG+jYEJyGMEABJTAg0KZW5kc3RyZWFtDWVuZG9iag10cmFpbGVyDTw8L1NpemUgNA0vSUQgWyhuXDM2NFxmXDIzNlwzMjVcMjYyfVwyNjRcMjcwXDM1MCpcMTc3XDAzN1wzNjdcMDA3XDMxNikgKG5cMzY0XGZcMjM2XDMyNVwyNjJ9XDI2NFwyNzBcMzUwKlwxNzdcMDM3XDM2N1wwMDdcMzE2KV0+Pg1zdGFydHhyZWYNMTgyDSUlRU9GDQ==";
Byte[] image = GetBytes(str);
{
Model.ImageData = image;
}
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
in view Bind it as
<img src="data:image;base64,#System.Convert.ToBase64String(Model.ImageData)" />
The image is not rendering , it is showing an image icon only in page .
Where it is going wrong .Please suggest .
Your str looks like it is already base64, so you don't need to dance to binary and back. You can just stick it in the view where it should be.
Decoding that string from base64 revealed that it is not an image, it's a pdf file.
So your api (3d party, I guess) serves you not an image.
And lastly to successfully use an image like that you probably need to specify mime type like this <img src="data:image/png;base64,some text representation of binary data" />
http://en.wikipedia.org/wiki/Data_URI_scheme
I did researches and fell on this solution: http://forums.xamarin.com/discussion/22682/is-there-a-way-to-turn-an-imagesource-into-a-byte-array
Initial question: http://forums.xamarin.com/discussion/29569/is-there-a-cross-platform-solution-to-imagesource-to-byte#latest
We want to upload an image through a HTTP Post, here's what we tried:
HttpClient httpClient = new HttpClient ();
byte[] TargetImageByte = **TargetImageSource**; //How to convert it to a byte[]?
HttpContent httpContent = new ByteArrayContent (TargetImageByte);
httpClient.PostAsync ("https://api.magikweb.ca/debug/file.php", httpContent);
We also are having a hard time with the libraries we gotta include in the using clauses. It seems like using System.IO; works, but it doesn't give us access to classes like FileInfo or FileStream.
Anybody has any idea how this can be done aside from custom platform-specific converters?
Possibly a Xamarin.Forms.ImageSource function toByte()?
Lemme know if you need more information.
TargetImageSource is a Xamarin.Forms.ImageSource.
ImageSource TargetImageSource = null;
Solution (Sten was right)
The ImageSource has to originate from another type to exist, that previous type can be converted to a byte[]. In this case, I use the Xamarin.Forms.Labs to take a picture and it returns a MediaFile in which a FileStream is accessible through the Source property.
//--Upload image
//Initialization
HttpClient httpClient = new HttpClient ();
MultipartFormDataContent formContent = new MultipartFormDataContent ();
//Convert the Stream into byte[]
byte[] TargetImageByte = ReadFully(mediaFile.Source);
HttpContent httpContent = new ByteArrayContent (TargetImageByte);
formContent.Add (httpContent, "image", "image.jpg");
//Send it!
await httpClient.PostAsync ("https://api.magikweb.ca/xxx.php", formContent);
App.RootPage.NavigateTo (new ClaimHistoryPage());
The function:
public static byte[] ReadFully(Stream input)
{
using (MemoryStream ms = new MemoryStream()){
input.CopyTo(ms);
return ms.ToArray();
}
}
I think you're looking at it a bit backwards.
ImageSource is a way to provide a source image for Xamarin.Forms.Image to show some content. If you're already showing something on the screen your Image view was populated with data that came from elsewhere, such as a file or resource or stored in an array in memory... or however else you got that in the first place. Instead of trying to get that data back from ImageSource you can keep a reference to it and upload it as needed.
Maybe you can elaborate a bit on your particular need if you don't feel this solution applies to your case.
Pseudo code:
ShowImage(){
ImageSource imageSource = ImageSource.FromFile("image.png"); // read an image file
xf_Image.Source = imageSource; // show it in your UI
}
UploadImage(){
byte[] data = File.ReadAll("image.png");
// rather than
// byte[] data = SomeMagicalMethod(xf_Image.Source);
HttpClient.Post(url, data);
}
UPDATE:
Since you're taking a picture you can copy the MediaFile.Source stream into a memory stream, then you can reset the memory stream's position to point at the beginning of the stream so that you can read it once again and copy it to the http body.
Alternatively you can store the MediaFile.Source to a file and use ImageSource.FromFile to load it in the UI, and when necessary - you can copy the file's contents into an http post body.
I am working on a project which requires to stored the 32x32 size Icons(png) in table. I am storing the image in table, but when I am getting it to display on page it feels something distorted. These are the png images so somewhere it could be transparent. But when I am showing that image in list of asp.Net page the transparent spaces are filled with black color.
Are there any ways/methods to store the png image in the table without distorting it's quality.
Thanks
This happens when you save Image to byte array with wrong ImageFormat. Use this code:
public byte[] imageToByteArray(string imagePath)
{
return imageToByteArray(System.Drawing.Image.FromFile(imagePath));
}
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
byte[] result = null;
using (MemoryStream ms = new MemoryStream())
{
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
result = ms.ToArray();
}
return result;
}
I am currently using a function inside an ASP.Net ASMX web service to generate a System.Drawing.Image. I would like to have this image (after converting to a png or something) passed to the browser as the reply to an AJAX call. (using jQuery's $.ajax). I've been Googling but can't seem to find out how, although I'm pretty sure it's possible. Do I convert it to a Data URI somehow first, or what?
I don't think you can do that. In order to return the image, you should have a method which returns bytearray which I don't think is of any use in javascript.
My workaround is to save the image in a certain location and return the address of the image to clientside.
Update: based on the comments, you can encode the image's data to Base64 string and handle the DATA URI on your ajax callback.
You can use JSON to represent the base64Encoded data, and on the ajax calklback, you can write something like:
$("#myImage").attr("src", "data:image/png;base64,RETURNED_DATA_FROM_ASMX");
Here's a sample code to convert your image to base64 in c#:
public string ImageToBase64(Image image,
System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
To find out how to format a DATA URI, you can visit http://en.wikipedia.org/wiki/Data_URI_scheme#Format.
i have an aspx page,there is textbox to write an url and a button to show some pictures that are in that url.I can load the url's source code to HtmlDocument.but i dont know how to load pictures from that html source code to show that pictures in my page.How can i do that ? Thanks in advance
You need to make the question more clear so that one can give you a specific answer.
HTML is a markup language which means that there are only format tags, there are no pictures embedded in a .html document. There are only links to images that are urls that can be accessed trough some address. In order to get the images you need to get that url.
If your question is how you can get the actual html from a link then refer to the following question. But, since you say that you can get the html, then you need to parse it using Regex or HTML Agility Pack.
Code to get the image:
byte[] imageData = DownloadData(Url); //DownloadData function from here
MemoryStream stream = new MemoryStream(imageData);
Image img = Image.FromStream(stream);
stream.Close();
for method DownloadData you can use WebClient or WebRequest to get the image in a byte array:
WebRequest req = WebRequest.Create("[URL here]");
WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();
byte[] b;
using (BinaryReader br = new BinaryReader(stream))
{
b = br.ReadBytes(size);
br.Close();
}
return b;