0

I am writing a program similar to TeamViewer. But I have a problem that the screen resolution is too much. How can I reduce the quality of the incoming picture?

byte[] ScreenShut()
{
    Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);

    Graphics gr = Graphics.FromImage(bmp);
    bmp.SetResolution(96.0F,96.0F);
    gr.CopyFromScreen(0, 0, 0, 0, new Size(bmp.Width, bmp.Height));
    MemoryStream ms = new MemoryStream();
    bmp.Save(ms, ImageFormat.Png);
    return ms.GetBuffer();
}
  • Have you tried `new Bitmap(Screen.PrimaryScreen.Bounds.Width / 2,Screen.PrimaryScreen.Bounds.Height / 2)`? Simply create a bitmap half of the size of screen resolution. Once this works, you can decide the percentage to reduce size. This is called auto-scalling. – Amit Joshi Mar 11 '17 at 11:57
  • When I do so, it takes a certain part of the screen. – Ömer Yılmazer Mar 11 '17 at 12:02
  • Ok; then go on with original code and resize the bitmap latter. Refer this: http://stackoverflow.com/q/10442269/5779732 – Amit Joshi Mar 11 '17 at 12:31

1 Answers1

1

Creating a bitmap from graphics.CopyFromScreen() then create another bitmap for scaling waste too much cpu.

[DllImport("user32")] static extern int GetDC(int hWnd);
[DllImport("user32")] static extern int ReleaseDC(int hwnd, int hDC)
[DllImport("gdi32")] static extern int StretchBlt(int hdc, int x, int y, int nWidth, int nHeight, int hSrcDC, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight, int dwRop);

        int W = Screen.PrimaryScreen.Bounds.Width;
        int H = Screen.PrimaryScreen.Bounds.Height;
        int dW = W * 2 / 3; // 66%
        int dH = H * 2 / 3; // 66%

        Bitmap img = new Bitmap(dW, dH);
        Graphics g = Graphics.FromImage(img);
        var dc = g.GetHdc();
        var screen = GetDC(0);
        StretchBlt(dc.ToInt32(), 0, 0, dW, dH, screen, 0, 0, W, H, 0xCC0020);
        g.ReleaseHdc(dc) ;
        ReleaseDC(0, screen)

        img.Save(@"C:\123.png", ImageFormat.Jpeg);

After scaling an image, text will become too hard to read. Scaling should be around 75%, then use JPG compression format to reduce size see here.