本文介绍了Image.Save例外" GDI +中发生一般性错误"保存到MemoryStream的时候的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器 - 客户端应用程序,我想从服务器获取的屏幕截图,但上线结果 bitmap.Save(MS,System.Drawing.Imaging.ImageFormat.Png) ; 结果我得到这个异常:在GDI +



专用插座ScreenSocket;
私人的MemoryStream毫秒;
公共无效ConnectScreenShot(IPEndPoint EP)
{
如果(ScreenSocket!= NULL)
{
ScreenSocket.Dispose();
ScreenSocket = NULL;
}
如果(MS!= NULL)
{
ms.Dispose();
毫秒= NULL;
}
ScreenSocket =新的Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
ScreenSocket.Connect(EP);
MS =新的MemoryStream();
矩形范围= Screen.GetBounds(Point.Empty);
使用(位图位图=新位图(bounds.Width,bounds.Height))
{使用
(图形G = Graphics.FromImage(位图))
{
g.CopyFromScreen(Point.Empty,Point.Empty,bitmap.Size);
}
bitmap.Save(MS,System.Drawing.Imaging.ImageFormat.Png);
}
}



这是为什么发生,如何将我解决这个问题?



更新:它的工作原理,当我使用 ImageFormat.Jpeg 而不是<$ C的$ C> ImageFormat.Png ,但我仍然需要一个PNG格式。


解决方案

它的工作我是这样的:




  • 当客户想要接收的截图,你需要传递开始前就知道图像尺寸。
    结果呼叫 GetScreenShotSize()来获得图像的大小。


  • 一旦你获得了尺寸,叫 GetScreenShot()来接收图像数据。




我用使用(MemoryStream的毫秒=新的MemoryStream())所以现在PNG格式工作。

 私人图片IMG = NULL; 
众长GetScreenShotSize()
{
矩形范围= Screen.GetBounds(Point.Empty);使用(BMP位图=新位图(bounds.Width,bounds.Height))
{
使用
(图形G = Graphics.FromImage(BMP))
{
g.CopyFromScreen(Point.Empty,Point.Empty,bounds.Size);
}使用
(MemoryStream的毫秒=新的MemoryStream())
{
bmp.Save(MS,System.Drawing.Imaging.ImageFormat.Png);
IMG = Image.FromStream(毫秒);
返回ms.Length;
}
}
}
公共无效GetScreenShot(IPEndPoint EP)
{$ B $使用B(Socket套接字=新的Socket(AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp))
{
socket.Connect(EP);
使用(MemoryStream的毫秒=新的MemoryStream())
{
img.Save(MS,System.Drawing.Imaging.ImageFormat.Png);
socket.Send(ms.ToArray(),SocketFlags.None);
}
img.Dispose();
IMG = NULL;
}
}


I have a server-client application, i want to get a Screen Shot from server,but on the line
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
i get this exception : A generic error occurred in GDI+.

    private Socket ScreenSocket;
    private MemoryStream ms;
    public void ConnectScreenShot(IPEndPoint ep)
    {
        if (ScreenSocket != null)
        {
            ScreenSocket.Dispose();
            ScreenSocket = null;
        }
        if (ms != null)
        {
            ms.Dispose();
            ms = null;
        }
        ScreenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        ScreenSocket.Connect(ep);
        ms = new MemoryStream();
        Rectangle bounds = Screen.GetBounds(Point.Empty);
        using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(Point.Empty, Point.Empty, bitmap.Size);
            }
            bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        }
    }

Why is that happening and how would i fix it?

Update: It works when i use ImageFormat.Jpeg Instead of ImageFormat.Png, but i still need a PNG format.

解决方案

it worked with me this way :

  • When a client wants to a receive a screenshot, you need to know image size before transferring starts.
    Call GetScreenShotSize() to get the size of the image.

  • Once you get the size, call GetScreenShot() to receive image data.

i used using (MemoryStream ms = new MemoryStream()) so now PNG format is working.

    private Image img = null;
    public long GetScreenShotSize()
    {
        Rectangle bounds = Screen.GetBounds(Point.Empty);
        using (Bitmap bmp = new Bitmap(bounds.Width, bounds.Height))
        {
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
            }
            using (MemoryStream ms = new MemoryStream())
            {
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                img = Image.FromStream(ms);
                return ms.Length;
            }
        }
    }
    public void GetScreenShot(IPEndPoint ep)
    {
        using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
        {
            socket.Connect(ep);
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                socket.Send(ms.ToArray(), SocketFlags.None);
            }
            img.Dispose();
            img = null;
        }
    }

这篇关于Image.Save例外&QUOT; GDI +中发生一般性错误&QUOT;保存到MemoryStream的时候的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:26