I have a problem with creating a screenshot of a scatterview. My screenshot always contains a black frame.
Here\'s my XAML-Code:
I found a quite simple way that works very well. Method TakeScreenshot
makes a screenshot an returns it as a bitmap. MakeScreenShotButton_Click
encodes the bitmap in jpeg, saves it, and sends it to the client as base64 String.
Here are the code snippets. First TakeScreenshot:
private Bitmap TakeScreenshot(int StartX, int StartY, int Width, int Height)
{
// Bitmap in right size
Bitmap Screenshot = new Bitmap(Width, Height);
Graphics G = Graphics.FromImage(Screenshot);
// snip wanted area
G.CopyFromScreen(StartX, StartY, 0, 0, new System.Drawing.Size(Width, Height), CopyPixelOperation.SourceCopy);
// save uncompressed bitmap to disk
string fileName = "C:\\TestBMP.bmp";
System.IO.FileStream fs = System.IO.File.Open(fileName, System.IO.FileMode.OpenOrCreate);
Screenshot.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp);
fs.Close();
return Screenshot;
}
And that's the MakeScreenShotButton_Click:
private void MakeScreenShotButton3_Click(object sender, RoutedEventArgs e)
{
// Start values
System.Windows.Point absolutPosition = ScatterViewScreenShot.TranslatePoint(new System.Windows.Point(0, 0), sw1);
System.Windows.Point screenPosition = this.PointToScreen(absolutPosition);
// Catch width and hight of scatterview
int widthScatterView = (int)ScatterViewScreenShot.ActualWidth;
int heightScatterView = (int)ScatterViewScreenShot.ActualHeight;
// Screenshot as bitmap TakeScreenshot(StartWert_X, StartWert_Y, BreiteBild, HöheBild)
Bitmap screen = TakeScreenshot((int)screenPosition.X, (int)screenPosition.Y, widthScatterView, heightScatterView);
MemoryStream ms = new MemoryStream();
try
{
//ImageCodecInfo - Object
ImageCodecInfo jpegCodec = null;
// Quality-Parameter
EncoderParameter qualitaetsParameter = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, 40L);
//Show available Codes in List
ImageCodecInfo[] alleCodecs = ImageCodecInfo.GetImageEncoders();
EncoderParameters codecParameter = new EncoderParameters(1);
codecParameter.Param[0] = qualitaetsParameter;
//Find Jpeg-Codec
//Codec-Info-Object
for (int i = 0; i < alleCodecs.Length; i++)
{
if (alleCodecs[i].MimeType == "image/jpeg")
{
jpegCodec = alleCodecs[i];
break;
}
}
// save image in stream
screen.Save(ms, jpegCodec, codecParameter);
}
catch (Exception k)
{
throw k;
}
// Convert image in Byte-Array
byte [] imageBytes = ms.ToArray();
// Convert Byte-Array in Base64String --> Image as String
string bildBase64 = Convert.ToBase64String(imageBytes);
TCP_Client client = new TCP_Client("192.168.5.3", 4321);
client.sendeNachricht(bildBase64);
}
It works without problems!
Cheers, Dennis