I'm making a basic application that share screens. To do that I need to save the screen as a bitmap and then convert it to an array to be sent to the other client. But when I try to convert the Image to bytearray I get this error: "A generic error occurred in GDI+." Code:
private void button1_Click(object sender, EventArgs e) { Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics g = Graphics.FromImage(bitmap); g.CopyFromScreen(0, 0, 0, 0, bitmap.Size); Image I = GetCompressedBitmap(bitmap, 40); byte[] Im = ImageToByteArray(I); SendBytes(Im); } public byte[] ImageToByteArray(Image img) { try { using (var stream = new MemoryStream()) { img.Save(stream, System.Drawing.Imaging.ImageFormat.Png); return stream.ToArray(); } } catch(Exception ex) { Console.WriteLine(ex.ToString()); return null; } } private Image GetCompressedBitmap(Bitmap bmp, long quality) { using (var mss = new MemoryStream()) { EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); ImageCodecInfo imageCodec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(o => o.FormatID == ImageFormat.Jpeg.Guid); EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = qualityParam; bmp.Save(mss, imageCodec, parameters); return Image.FromStream(mss); } }
Why does this occur and how can I solve this?
https://stackoverflow.com/questions/66740171/error-when-saving-screen-from-image-to-bytearray March 22, 2021 at 11:59AM
没有评论:
发表评论