Reputation: 34198
i am working with MDI application. before minimize any sdi form i want to capture it's screen shot without title bar.my code is working but the way i am capturing image that is not clear rather bit obscure. this way i do it. here is my code.
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_COMMAND && m.WParam.ToInt32() == SC_MINIMIZE)
{
OnMinimize(EventArgs.Empty);
}
base.WndProc(ref m);
}
protected virtual void OnMinimize(EventArgs e)
{
if (_lastSnapshot == null)
{
_lastSnapshot = new Bitmap(100, 100);
}
using (Image windowImage = new Bitmap(ClientRectangle.Width, ClientRectangle.Height))
using (Graphics windowGraphics = Graphics.FromImage(windowImage))
using (Graphics tipGraphics = Graphics.FromImage(_lastSnapshot))
{
Rectangle r = this.RectangleToScreen(ClientRectangle);
windowGraphics.CopyFromScreen(new Point(r.Left, r.Top), Point.Empty, new Size(r.Width, r.Height));
windowGraphics.Flush();
float scaleX = 1;
float scaleY = 1;
if (ClientRectangle.Width > ClientRectangle.Height)
{
scaleY = (float)ClientRectangle.Height / ClientRectangle.Width;
}
else if (ClientRectangle.Height > ClientRectangle.Width)
{
scaleX = (float)ClientRectangle.Width / ClientRectangle.Height;
}
tipGraphics.DrawImage(windowImage, 0, 0, 100 * scaleX, 100 * scaleY);
}
}
so guide me how should i get the snap of the sdi form which will be better clear and prominent. any idea. thanks.
Upvotes: 1
Views: 581
Reputation: 2572
You scale the picture and any scaling - no matter of up or down-scaling - will lead to a lower quality image. Instead of scaling the image I would get the width and height of the window, create a new bitmap with that size and finally draw the image with the same size as well.
protected virtual void OnMinimize(EventArgs e)
{
Rectangle r = this.RectangleToScreen(ClientRectangle);
if (_lastSnapshot == null)
{
_lastSnapshot = new Bitmap(r.Width, r.Height);
}
using (Image windowImage = new Bitmap(r.Width, r.Height))
using (Graphics windowGraphics = Graphics.FromImage(windowImage))
using (Graphics tipGraphics = Graphics.FromImage(_lastSnapshot))
{
windowGraphics.CopyFromScreen(new Point(r.Left, r.Top), new Point(0, 0), new Size(r.Width, r.Height));
windowGraphics.Flush();
tipGraphics.DrawImage(windowImage, 0, 0, r.Width, r.Height);
}
}
or something close to the above - I have not actually been able to test it.
Upvotes: 1