so .. am creating a Simple Desktop Viewer application.
but i keep getting this System.outOfMmemoryException when ever i try to de-serialize sent images through the stream.
the sending code :
private Image getScreen() {
Rectangle bound = Screen.PrimaryScreen.Bounds;
Bitmap ScreenShot = new Bitmap(bound.Width,bound.Height,PixelFormat.Format32bppArgb );
Graphics image = Graphics.FromImage(ScreenShot);
image.CopyFromScreen(bound.X,bound.Y,0,0,bound.Size,CopyPixelOperation.SourceCopy);
return ScreenShot;
}
private void SendImage() {
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(stream, getScreen());
}
note that the images are sent using a timer that calls the SendImage() once it's started
this is my Recieving Code :
BinaryFormatter binFormatter = new BinaryFormatter();
Image img = (Image)binFormatter.Deserialize(stream);
picturebox1.Image=img;
so .. whats Wrong?
Most likely this caused by a wrong usage of Stream. The code below is working fine:
private Image getScreen() {
Rectangle bound = Screen.PrimaryScreen.Bounds;
Bitmap ScreenShot = new Bitmap(bound.Width,bound.Height,PixelFormat.Format32bppArgb );
Graphics image = Graphics.FromImage(ScreenShot);
image.CopyFromScreen(bound.X,bound.Y,0,0,bound.Size,CopyPixelOperation.SourceCopy);
return ScreenShot;
}
private void SendImage(Stream stream) {
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(stream, getScreen());
}
void Main()
{
byte[] bytes = null;
using (var ms = new MemoryStream()) {
SendImage(ms);
bytes = ms.ToArray();
}
var receivedStream = new MemoryStream(bytes);
BinaryFormatter binFormatter = new BinaryFormatter();
Image img = (Image)binFormatter.Deserialize(receivedStream);
img.Save("c:\\temp\\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
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);
}
I have two functions: one to convert from image to byte and other to convert from byte to bitmapImage.
So, when I open the window with that images, I convert from byte to bitmapImage and it works great, but when I close and open it again it just keeps on memory and if I continue to do that time and time again it just throws an exception Out Of Memory exception
Image to byte->
private byte[] ConvertImageToBinary(Image img)
{
using (MemoryStream ss = new MemoryStream())
{
img.Save(ss, System.Drawing.Imaging.ImageFormat.Jpeg);
var s = ss.ToArray();
var jpegQuality = 50;
Image image;
using (var inputStream = new MemoryStream(s))
{
image = Image.FromStream(inputStream);
var jpegEncoder = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders()
.First(c => c.FormatID == System.Drawing.Imaging.ImageFormat.Jpeg.Guid);
var encoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, jpegQuality);
Byte[] outputBytes;
using (var outputStream = new MemoryStream())
{
image.Save(outputStream, jpegEncoder, encoderParameters);
return outputBytes = outputStream.ToArray();
}
}
}
}
Byte to bitmap ->
public BitmapImage ConvertBinaryToImage(byte[] array)
{
var image = new BitmapImage();
using (var ms = new MemoryStream(array))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad; // here
image.StreamSource = ms;
image.EndInit();
image.Freeze();
}
return image;
}
When I open the WindowDragAndDrop it loads all the images
But when I close it it still uses the same amount of memory
Image is indeed disposable (https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image?view=netframework-4.8), so you also need:
using (var image = Image.FromStream(inputStream)){
}
Around everywhere you use Image objects.
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 got the new image successfully, but I can't get the original image, the image was cropped.
Here is the code I tried:
private byte[] ConvertToCCITT4(byte[] input)
{
MemoryStream memoryStream1 = new MemoryStream(input);
RasterCodecs.CodecsPath = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
RasterCodecs rasterCodecs = new RasterCodecs();
using (IRasterImage irasterImage = rasterCodecs.Load((Stream)memoryStream1))
{
MemoryStream memoryStream2 = new MemoryStream();
rasterCodecs.Save(irasterImage, (Stream)memoryStream2, (RasterImageFormat)29, 1);
return memoryStream2.ToArray();
}
}
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");