Hi i want to convert bitmap image to base64png format i tried using the following code.i don't think the code works good enough.Any suggestions will be appreciated.
Bitmap immagex = signature;
MemoryStream baos = new MemoryStream();
immagex.Compress(Bitmap.CompressFormat.Png, 100, baos);
byte[] b = baos.ToArray();
String Base64Encodestring = Base64.EncodeToString(b, Base64.Default);
I belive this would do the trick.
string tempBase64 = Convert.ToBase64String(b);
Goodluck.
Related
I have a jpg strign stored in "(string)HttpContext.Current.Session["image" + 0];" that I am trying to convert to a System.Drawing.Image.
I know the string is correct, because when I do this -
img.Src = "data:image/jpg;base64," + (string)HttpContext.Current.Session["image" + 0];
Everying works as intended.
But when I do this -
string inputString = (string)HttpContext.Current.Session["image" + 0];
byte[] imageBytes = Convert.FromBase64String(inputString);
using (MemoryStream ms = new MemoryStream(imageBytes))
{
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
}
I get and error telling me the format is incorrect when trying to create an image from the stream.
Please help
don't you need to convert webp to jpg/png first?
for example WebP-wrapper
using (WebP webp = new WebP())
{
var image = webp.Decode(imageBytes );
}
I've found this sample code to convert an image to base 64, I'm not sure how to pass an image into it though. I want to be able to give a path to a specific directory. I've managed to find a file with this code:
byte[] ImageData = File.ReadAllBytes("storage/emulated/0/DCIM/Camera/img.jpg");
But I need to pass that into the following code.
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;
}
}
Hope you can help. Thanks.
byte[] ImageData = File.ReadAllBytes(path_to_file);
string base64String = Convert.ToBase64String(ImageData);
I have an image string contain in an XML file and want to convert the string into an image. I have used the following code which is giving error: Parameter is not valid.
Code is following which is give above error:
Byte[] data = new Byte[0];
byte[] array = Encoding.ASCII.GetBytes("D:\notimg.txt");
MemoryStream mem = new MemoryStream(array);
pictureBox1.InitialImage = null;
pictureBox1.Image = Image.FromStream(mem);
Below is the data contained in XML tag which I need to convert into the image and display into picture box, I have placed the below data into a notepad file and removed the XML tag . Please, anyone, guide how to display the below data into an image. Thanks in advance.
<photograph>/9j/4AAQSkZJRgABAQECWAJYAAD/4QBuRXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAZKGAAcAAAA6AAAALAAAAABVTklDT0RFAABDAHIAZQBhAHQAZQBkACAAYgB5ACAAQQBjAGMAdQBTAG8AZgB0ACAAQwBvAHIAcAAu/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgBwgFeAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8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v940xjs4HBINKST940wAfSlBI44FAIcB2HFSKvuajU4z81OXGeTQth2JVOPTiplGccZqEoNu5WP50qSFOWUsB6HFAiflT8v5UqzA9SUbpVd7mNum5SPU803zMcsA49RSuBZ80g4Kqw/I07tvjOR3HcVSEmMN1/pUgf8AjTgjqKdwLKzbSCDwavRyCWPb0PashmzkgY3c49DU0EpRgc0MDuvBuo+VLJA7YyQMHpXbtd26j5pUX/eOK8aW7ltnE8LFGPBI7e9Oi1CfzDIJnDk5LE5Jouw1PY47y2lbakyMR6GrFeRw63fRtzMWHowyK6TSvE7p8jnHoGPy/wD1qaYJncUVTtL2O5UbflbGdpPb1B7irlUUgooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooqreXSWdpJO7BVRc5Jo2Fexna1qSWNpJIxG2PgDP3m7D8K8mvbvzJ5JnYu7nOSa0df1t9Um2odsMfTPc9ya52TG/nJrNa6krUa8vOf6Uwle/HvTWbsKYSTTGPLxjsW/HFAc5wFA96YBk+n1qTAUcc0gsOyxPapETPXg01flALbgPYU/wA4IRsBB9xmqDYRo2QZ2Fu9K0kQQgR4z69qYJJSSFC/yqMXbq3Qj2IyKWwD12gZ2kD+8vOKcueiPuBpquGOUOxsdu/4U/y2XGR15BFK4xsg5+dcEUi5Qe3oalXdnnml8vPNK4WIjjqBipFbB46U4Rd6csXQY5ouOwh42jtyRTgnzcdKVozwOmKkRe350XsFhwOUIPpTF4OAasqi46HNQSIQSF4+lCkKxLFICQA+D71bjlIPzZ+orMC4PuPep4d+OOcenWqFY6Kx1i4tCmx9yqcgZxiu+0fWIdTh+VgJUHzLXlCSEY7+xFX7DUJbOdZoppRRVDR//2Q==</photograph>
Quick and dirty:
var s = File.ReadAllText(#"d:\file.txt");
s = s.Replace("<photograph>", string.Empty).Replace("</photograph>", string.Empty);
var b = Convert.FromBase64String(s);
using (var mem = new MemoryStream(b))
{
pictureBox1.Image = Image.FromStream(mem);
}
It's Base64
first convert to byte[]
then use
public System.Drawing.Image Base64ToImage(byte[] imageBytes)
{
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
return image;
}
then You Will Got image..!:)
I have this code, it works very well in android studio but not in xamarin
bitmap.Compress() has different arguments in xamarin and i am confused how to convert image into base64 or binary in xamarin.android?
I am receving an error in the 3rd line:
( bitmap.Compress() has some invalid arguments).
Bitmap bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.ace1);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100,bao);
byte[] ba = bao.ToByteArray();
string bal = Base64.EncodeToString(ba, Base64.Default);
If you look at the documentation for Bitmap.Compress in Xamarin, you'll see that the last parameter is a Stream.
The equivalent of ByteArrayOutputStream in .NET is MemoryStream, so your code would be:
Bitmap bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.ace1);
MemoryStream stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
byte[] ba = stream.ToArray();
string bal = Base64.EncodeToString(ba, Base64Flags.Default);
(You could use Convert.ToBase64String instead of Base64.EncodeToString if you wanted, too.)
This is how I'm getting a Byte[] for my Bitmap object:
Byte[] imageArray = null;
Bitmap selectedProfilePic = this.GetProfilePicBitmap ();
if (selectedProfilePic != null) {
using (var ms = new System.IO.MemoryStream ()) {
selectedProfilePic.Compress (Bitmap.CompressFormat.Png, 0, ms);
imageArray = ms.ToArray ();
}
}
Hope this helps.
Hello friends i want to "convert a system.windows.control.image" to "System.Drawing.Image",but i am unable to do so.I am using below code for this
var e = (MouseButtonEventArgs)sender;
var device = e.MouseDevice.DirectlyOver;
System.Windows.Controls.Image img = (System.Windows.Controls.Image)device;
I have "img" i.e of type "system.windows.control.image" i need to convert it to bitmap or drawing type.
Try this....
Use your img variable in this code...
MemoryStream ms = new MemoryStream();
System.Windows.Media.Imaging.BmpBitmapEncoder bbe = new BmpBitmapEncoder();
bbe.Frames.Add(BitmapFrame.Create(new Uri(img.Source.ToString(),UriKind.RelativeOrAbsolute)));
bbe.Save(ms);
System.Drawing.Image img2 = System.Drawing.Image.FromStream(ms);
button1.Image = img2;
convert from byte[] to image
MemoryStream ms = new MemoryStream(imageByte);
Image image = Image.FromStream(ms);
it may help you..