i've tried the following code for loading an image in picturebox from database. but everytime , i get an error like 'paramater is not valid'.
buttonSave()
{
.......
.......
img = Image.FromFile(strFileName);
byte[] byteImg = ImageToByteArray(img);
objEmp.Picture = byteImg;
.......
.......
}
public byte[] ImageToByteArray(Image img)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
Display()
{
.......
.......
Byte[] bytePicData = (Byte[])dt.Rows[0]["PICTURE"];
MemoryStream stmPicData = new MemoryStream(bytePicData);
PicBox.Image = Bitmap.FromStream(stmPicData);}
.......
.......
}
the image is corrupt. The error is from the FromStream method. Can you write to disk and see if you can open it in an image view. If not then check the code where you are inserting it into the database
Byte[] bytePicData = (Byte[])dt.Rows[0]["PICTURE"];
// Save
File.WriteAllBytes("out.bmp", bytePicData);
MemoryStream stmPicData = new MemoryStream(bytePicData);
PicBox.Image = BitMap.FromStream(stmPicData);
Related
i used PDFLIBNET to convert pdf to image :
public void ConvertPDFtoPNG(string filename, String dirOut)
{
try
{
PDFLibNet.PDFWrapper _pdfDoc = new PDFLibNet.PDFWrapper();
_pdfDoc.LoadPDF(filename);
System.Drawing.Image img = RenderPage(_pdfDoc, 0);
img.Save(Path.Combine(dirOut, Path.GetFileNameWithoutExtension(filename) + ".png"));
_pdfDoc.Dispose();
return;
}
catch
{
File.Copy(System.IO.Path.Combine(Environment.CurrentDirectory, "0.png"), Path.GetFileNameWithoutExtension(filename) + ".png");
}
}
This code is working properly
But i need to use in image handler without saving image
I change this code to use MemoryStream but get gray image :
public string ConvertPDFtoPNG(string filename)
{
PDFLibNet.PDFWrapper _pdfDoc = new PDFLibNet.PDFWrapper();
_pdfDoc.LoadPDF(filename);
System.Drawing.Image img = RenderPage(_pdfDoc, 0);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
_pdfDoc.Dispose();
base64String = Convert.ToBase64String(ms.ToArray(), 0, ms.ToArray().Length);
}
please help me
thanks
Try this:
var b = File.ReadAllBytes(filename);
using (var ms = new MemoryStream(b))
{
var i = Image.FromStream(ms);
i.Save(ms, ImageFormat.Jpeg);
}
The last line of the following code gives the error "Parameter is not valid", when the original image is an SVG:
var imageBytes = Convert.FromBase64String(imageBase64String);
var memStream = new MemoryStream(imageBytes);
//memStream.Seek(0, SeekOrigin.Begin);
var imageObject = new Bitmap(memStream);
Help please. Thanks.
EDIT: The image for example I am using is the image of the first formula in the following page right under the Theoremsection:
https://en.wikipedia.org/wiki/Green%27s_theorem
Can you try with 'using' for memorystream to handle garbage collection by itself?
var imageBytes = Convert.FromBase64String(imageBase64String);
Bitmap m = ByteToBitmap(imageBytes);
public static Bitmap ByteToBitmap(byte[] imageByte)
{
using (MemoryStream mStream = new MemoryStream())
{
mStream.Write(imageByte, 0, imageByte.Length); // this will stream dataand handle image length by itself
mStream.Seek(0, SeekOrigin.Begin);
Bitmap bm = new Bitmap(mStream);
return bm;
}
}
For SVG,
public static Bitmap ByteToBitmap(byte[] imageByte)
{
using (MemoryStream mStream = new MemoryStream(imageByte))
{
var s= SvgDocument.Open(mStream);
var bm= svgDocument.Draw();
return bm;
}
}
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
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
I am trying to extract a BitmapImage from a JPG. This is the code I have:
FileStream fIn = new FileStream(sourceFileName, FileMode.Open); // source JPG
Bitmap dImg = new Bitmap(fIn);
MemoryStream ms = new MemoryStream();
dImg.Save(ms, ImageFormat.Jpeg);
image = new BitmapImage();
image.BeginInit();
image.StreamSource = new MemoryStream(ms.ToArray());
image.EndInit();
ms.Close();
image comes back with a 0 × 0 image, which of course means it didn't work. How do I do this?
Try this:
public void Load(string fileName)
{
using(Stream BitmapStream = System.IO.File.Open(fileName,System.IO.FileMode.Open ))
{
Image img = Image.FromStream(BitmapStream);
mBitmap=new Bitmap(img);
//...do whatever
}
}
Or you can just do this (source):
Bitmap myBmp = Bitmap.FromFile("path here");