BinaryFormatter.Deserlize 对于 Bitmap raises a System.OutOfMemoryException
BinaryFormatter.Deserlize for a Bitmap rases a System.OutOfMemoryException
所以 .. 正在创建一个简单的桌面查看器应用程序。
但每当我尝试通过流反序列化发送的图像时,我都会收到此 System.outOfMmemoryException。
发送代码:
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());
}
请注意,图像是使用计时器发送的,该计时器在启动后调用 SendImage()
这是我的接收码:
BinaryFormatter binFormatter = new BinaryFormatter();
Image img = (Image)binFormatter.Deserialize(stream);
picturebox1.Image=img;
所以 .. 怎么了?
这很可能是 Stream 的错误使用造成的。下面的代码工作正常:
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);
}
所以 .. 正在创建一个简单的桌面查看器应用程序。
但每当我尝试通过流反序列化发送的图像时,我都会收到此 System.outOfMmemoryException。
发送代码:
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());
}
请注意,图像是使用计时器发送的,该计时器在启动后调用 SendImage()
这是我的接收码:
BinaryFormatter binFormatter = new BinaryFormatter();
Image img = (Image)binFormatter.Deserialize(stream);
picturebox1.Image=img;
所以 .. 怎么了?
这很可能是 Stream 的错误使用造成的。下面的代码工作正常:
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);
}