private ImageCodecInfo GetEncoder(System.Drawing.Imaging.ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) { return(codec); } } return(null); }
/// <summary> /// Process the capture based on the requested format. /// </summary> /// <param name="width">image width</param> /// <param name="height">image height</param> /// <param name="pitch">data pitch (bytes per row)</param> /// <param name="format">target format</param> /// <param name="pBits">IntPtr to the image data</param> /// <param name="request">The original requets</param> protected void ProcessCapture(int width, int height, int pitch, PixelFormat format, IntPtr pBits, ScreenshotRequest request) { if (request == null) { return; } if (format == PixelFormat.Undefined) { DebugMessage("Unsupported render target format"); return; } // Copy the image data from the buffer int size = height * pitch; var data = new byte[size]; Marshal.Copy(pBits, data, 0, size); // Prepare the response Screenshot response = null; if (request.Format == ImageFormat.PixelData) { // Return the raw data response = new Screenshot(request.RequestId, data) { Format = request.Format, PixelFormat = format, Height = height, Width = width, Stride = pitch }; } else { // Return an image using (var bm = data.ToBitmap(width, height, pitch, format)) { System.Drawing.Imaging.ImageFormat imgFormat = System.Drawing.Imaging.ImageFormat.Bmp; switch (request.Format) { case ImageFormat.Jpeg: imgFormat = System.Drawing.Imaging.ImageFormat.Jpeg; break; case ImageFormat.Png: imgFormat = System.Drawing.Imaging.ImageFormat.Png; break; } response = new Screenshot(request.RequestId, bm.ToByteArray(imgFormat)) { Format = request.Format, Height = bm.Height, Width = bm.Width }; } } // Send the response SendResponse(response); }