C# storing Byte[] and retrieving it from MySQL - c#

I'm inserting the byte[] file into MySQL BLOB column, but I can not show the image when retrieving. I have one function to store the image as byte[]
public byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
Which I call this way:
Image img = Image.FromFile('trend.jpg');
#field = imageToByteArray(img);
In PhpMyAdmin it shows this way
On the other hand, when I try to retrieve the image into a picturebox, I receive an error.
byte[] bytes = (byte[])item["image"];
pictureBox1.Image = byteArrayToImage(bytes);
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
Invalid character in a base-64

Related

Value cannot be null. Parameter name: encoder in bitmap casting c#

In order to convert bitmap to base64 i have to convert my bitmap to Image
I get this message when i'm converting an image to memorystream as img.Save(ms, img.RawFormat); after casting my image from a screenshoot bitmap like Image img = (Image)bitmap; or Image img = bitmap as Image, but it's working fine when i use local stored image like Image img = Image.FromFile(Path).
how can i avoid this error while i don't want to store the screenshoot and read it again each time
this is the code i have tried
Image img = bitmap as Image;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, img.RawFormat);
string base64 = Convert.ToBase64String(ms.ToArray());
}
Actually, you don't have to convert Bitmab to Image while you can encode your Bitmab with Base64 directly
try this:
using (MemoryStream ms = new MemoryStream())
{
bitmab.Save(ms, ImageFormat.Jpeg); // you can change your image format as you want
byte[] imageBytes = ms.ToArray();
string base64 = Convert.ToBase64String(imageBytes);
}

When i try to convert base64string to Image in c#

When i try to convert a base64string to an Image in C#, I'm getting output as "System.Drawing.Bitmap" instead of the actual Image:
public Image DownFile(string base64String)//string file
{
//Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
//Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
image.Save("E:/Project Utilitie Connection/FileDownloadTask/Images", System.Drawing.Imaging.ImageFormat.Jpeg);
return image;
}
try a using statement for the MemoryStream, like so:
byte[] bytes = Convert.FromBase64String(base64String);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
return image;
The framework is converting the Image object to string which is why you are seeing System.Drawig.Bitmap
Create a response, giving it the bytes from the converted base64 string and then setting the content type for response so the client will know how to render the content.
public class ValuesController : ApiController {
[HttpGet]
public IHttpActionResult DownFile(string base64String) {
if (!string.IsNullOrWhiteSpace(base64String)) {
byte[] imageBytes = Convert.FromBase64String(base64String);
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new ByteArrayContent(imageBytes);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
return ResponseMessage(response);
}
return BadRequest();
}
}
This is a very simplified example to demonstrate how it can be done. Take some time to review and understand what was done so that it can be implemented into your particular scenario.

How can i get an imagefrom the web with headers?

How can i get in c# an image as bitmapimage from an url and pass to it some headers? (not parameters, headers)
And also i found a simple solution but i couldn't understand why it was wrong, the problem is that:
WHen i get byte[] from the server i get the image with a lot of question marks, this appends i thinks because of a different text encoding, how can i fix the code so i can get successfully the image from c# with headers and visible to bitmap?
You have to convert your image into a base64 encoded string and pass it to the headers.
using (Image image = Image.FromFile(Path))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
Then on the other side you can parse it and convert it back to an image:
public Image LoadImage()
{
//data:image/png;base64,
byte[] bytes = Convert.FromBase64String(YOUR_BASE64_ENCODED_STRING);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
return image;
}

Covert byte data to Image

I am beginner in .net.I am trying to convert from byte to image format.I am cosing error
My code is
MemoryStream ms = new MemoryStream(byt);
Image returnImage = Image.FromStream(ms);
returnImage.Save("D:\\image.jpg");
Occuring error in line " Image returnImage = Image.FromStream(ms);" as "Parameter is not valid."
Please try below Code
public static void byteArrayToImage(byte[] data)
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
{
Image img=new Bitmap(Image.FromStream(ms));
img.Save(#"D:\image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}

Converting byte to image and image to byte using C#

currently i am working with ASP.NET and C# to store image into MySQL (using blob datatype). I'm storing it successfully into database, but now problem is, How can I retrieve that byte[] to image format ?
FUNCTION: code to convert byte[] to image
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms); --> here gives me error as `parameter is not valid`
return returnImage;
}
retieved as datatable...
if (dt1.Rows.Count > 0)
{
byteArrayToImage((byte[]) dt1.Rows[0]["PortfolioSlideImages"]);
//MemoryStream ms = new MemoryStream((byte[])dt1.Rows[0]["PortfolioSlideImages"]);
//Image returnImage = Image.FromStream(ms);
//return returnImage;
}
Try this function,
public Image byteArrayToImage(byte[] byteArrayIn)
{
Image returnImage = null;
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
returnImage = Image.FromStream(ms);
}
return returnImage;
}
for more info, visit this link

Categories