I have a 63572 x 63573 High resolution JPG image.
I want to convert the image to byte array and convert byte array to OpenCvsharp.Mat.
the image is grayscale.
I tried to way that below code. However, It occurs 'System.OverflowException'(System.Drawing.dll).
System.Drawing.Image image = System.Drawing.Image.FromFile(filepath);
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
var data = ms.ToArray();
So, I tried to way that below code. However,
It didn't run properly.
var data = File.ReadAllBytes(filepath);
using (Mat mat = new Mat(height, width, MatType.CV_8UC1, data))
{
...
}
Related
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);
}
This may be an existing question but the answers I got is not exactly what I'm looking for.
In C# Winforms, I want to convert the image (not the path) from the picturebox and convert it into Byte array and display that Byte array in label.
Currently, this is what I have.
Byte[] result = (Byte[]) new ImageConverter().ConvertTo(pbOrigImage.Image, typeof(Byte[]));
Then, after displaying the Byte array in label, I want to convert it from Byte array to image. I currently don't have codes when it comes to image to Byte array conversion. But is this possible?
You can use the following methods for conversion from byte[] to Image,
public byte[] ConvertImageToBytes(Image img)
{
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
Bitmap bmp = new Bitmap(img);
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
arr = ms.ToArray();
}
return arr;
}
public Image ConvertBytesToImage(byte[] arr)
{
using (MemoryStream ms = new MemoryStream(arr))
{
return Bitmap.FromStream(ms);
}
}
To convertbyte[] to a string or vice-versa, you can refer to this
Hi I wanna convert binary array to bitmap and show image in a picturebox. I wrote the following code but I got exception that says that the parameter is not valid .
public static Bitmap ByteToImage(byte[] blob)
{
MemoryStream mStream = new MemoryStream();
byte[] pData = blob;
mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
Bitmap bm = new Bitmap(mStream);
mStream.Dispose();
return bm;
}
It really depends on what is in blob. Is it a valid bitmap format (like PNG, BMP, GIF, etc?). If it is raw byte information about the pixels in the bitmap, you can not do it like that.
It may help to rewind the stream to the beginning using mStream.Seek(0, SeekOrigin.Begin) before the line Bitmap bm = new Bitmap(mStream);.
public static Bitmap ByteToImage(byte[] blob)
{
using (MemoryStream mStream = new MemoryStream())
{
mStream.Write(blob, 0, blob.Length);
mStream.Seek(0, SeekOrigin.Begin);
Bitmap bm = new Bitmap(mStream);
return bm;
}
}
Don't dispose of the MemoryStream. It now belongs to the image object and will be disposed when you dispose the image.
Also consider doing it like this
var ms = new MemoryStream(blob);
var img = Image.FromStream(ms);
.....
img.Dispose(); //once you are done with the image.
System.IO.MemoryStream mStrm = new System.IO.MemoryStream(your byte array);
Image im = Image.FromStream(mStrm);
im.Save("image.bmp");
Try this. If you still get any error or exception; please post your bytes which you are trying to convert to image. There should be problem in your image stream....
Hi I can’t find sample for convert MULTI PAGE tiff image to byte array.
For convert byte array to Tiff I use this method
public static Tiff CreateTiffFromBytes(byte[] bytes)
{
using (var ms = new MemoryStream(bytes))
{
Tiff tiff = Tiff.ClientOpen("in-memory", "r", ms, new TiffStream());
return tiff;
}
}
EDITED:
This method convert TIFF image with more pages to byte Array. I think in this method will be root of problem.
//imageIn is tif image with 12 pages
public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
using (var ms = new MemoryStream())
{
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Tiff);
return ms.ToArray();
}
}
public static List<System.Drawing.Image> GetAllPages(System.Drawing.Image multiTiff)
{
var images = new List<System.Drawing.Image>();
var bitmap = (Bitmap)multiTiff;
int count = bitmap.GetFrameCount(FrameDimension.Page);
for (int idx = 0; idx < count; idx++)
{
bitmap.SelectActiveFrame(FrameDimension.Page, idx);
using (var byteStream = new MemoryStream())
{
bitmap.Save(byteStream, ImageFormat.Tiff);
images.Add(System.Drawing.Image.FromStream(byteStream));
}
}
return images;
}
After conversion to byte array I lose pages.
Image from byte array has only one page.
Image src = Image.FromFile(source);
//imagesInSource.Count (pages) is 12
List<Image> imagesInSource = GetAllPages(src);
byte[] imageData = ImageToByteArray(src);
Image des = ImageConvert.ByteArrayToImage(imageData);
//imagesInSource.Count (pages) is 1
List<Image> imagesInDes = GetAllPages(des);
I am not sure why you can't send TIFF file to the service? The file is just bytes, after all.
And your code in the first snippet is incorrect because you dispose memory stream that is passed to Tiff object. You shouldn't do that. The Tiff object will dispose the stream itself.
EDIT:
In the third snippet you create images for each page of the System.Drawing.Image but the you convert only first produced image to byte array. You should use something like
List<byte[]> imagesBytes = new List<byte[]>();
foreach (Image img in imagesInSource)
{
byte[] imageData = ImageToByteArray(src);
imageBytes.Add(imageData);
}
Then you should send imagesBytes to your server and create several TIFF images from that.
Anyway, it seems like you should think more about what are you really trying to do. Because for now it unclear to me what all these conversions are for.
I need to convert System.Windows.Controls.Image to byte[].
How to do it ?
Assuming that you answer my query above that you are actually trying to convert an image into a byte array rather than an image control into a byte array here is the code to do so;
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
In general, to convert an object to a byte array, you may want to use binary serialization. This will generate a memory stream from which you can extract a byte array.
You may start with this Question and Answer
System.Windows.Controls.Image img = new System.Windows.Controls.Image();
var uri = new Uri("pack://application:,,,/Images/myImage.jpg");
img.Source = new BitmapImage(uri);
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
var bmp = img.Source as BitmapImage;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(ms);
arr = ms.ToArray();
}