English 中文(简体)
图像。 拯救记忆犹Stream时发生的通用错误
原标题:Image.Save exception "A generic error occurred in GDI+." when saving to MemoryStream
  • 时间:2012-05-14 02:47:05
  •  标签:
  • c#
  • image
  • gdi+

我有服务器-客户应用程序,即希望从服务器上获得一个筛选数据,而放在以下线上:
<>>>>> 轨道图.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
。 i 收到这一例外:A 通用错误发生在全球发展指数+.

    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);
        }
    }

为什么会发生这种情况?

<>Update: 它使用<代码>。 Jpeg 不用<代码>。 Png,但仍然需要一种PNG格式。

最佳回答

它以这种方式与我合作:

  • 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.

  • 一旦获得图像数据,请打电话GetScreenShot(

i 使用的<代码>使用(MemoryStream ms = 新的记忆Stream(),因此,现在巴布亚新几内亚的格式正在运行。

    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;
        }
    }
问题回答

你说:

i 想从服务器处获得一个屏幕垫。

与此相关的错误是,正在使用该守则的背景或许是服务背景(例如国际会计系统),而不是桌面。 这是否正确? 如果是的话,你会以什么方式返回?

为记录起见,以下(部委)代码行重:

    var 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);
    }

。 因此,我认为,你需要提供资料,说明这一法典在什么地方和什么地方运作。

我知道这是一个老的职位,但我想补充这一信息,如果像我这样的人将来会处理这个问题。

我有幸拯救了。 国民党的形象和形象,因为国际移民协会的财团账户没有获得这一关键内容。

KongEY_CLASSES_ROOTCLSID{FAE3D380-FEA4-4623-8C75-C6B61110B681}

当你使用png图表时,需要做些什么。

对我来说,我使用<代码>。 图像。 Save(Stream, ImageCodec) http://www.un.org/french/ga/president GDI+错误中出现了通用错误。

我正在尝试使用<条码>EncoderPara公尺,以节省100%的质量。 这完全是在“米机器”(do!)上,而不是在生产上。

当我使用<代码>时。 Save(Stream, ImageFormat) 相反,错误消失! 因此,我像一个主讲人一样,继续使用后者,尽管它以我假定为50%的违约质量来挽救他们。

希望会帮助人们。

abrirConexion();
cmd = new SqlCommand(" INSERT INTO [SICRE_BI].[Credito_CCSS].[bi_img](Descripcion,img) VALUES (@nom_img,@img)",cn);                                
cmd.Parameters.Add("@Descripcion", SqlDbType.NChar);
cmd.Parameters.Add("@img", SqlDbType.Image);

cmd.Parameters["@Descripcion"].Value = descripcion;

System.IO.MemoryStream ms = new System.IO.MemoryStream();

pbImagen.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//Aqui se me cae.Aqui se me cae.Aqui .

cmd.Parameters["@img"].Value = ms.GetBuffer();    //ms.GetBuffer();

cmd.ExecuteNonQuery();
return true;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签