Autor Beitrag
MartinBush
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 46



BeitragVerfasst: Di 11.11.08 14:10 
Mit folgendem Code mache ich einen Screenshot meines Bildschirms (CF)

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
            public static void Snapshot(string fileName, Rectangle rectangle)
            {
                //Use a zeropointer to get hold of the screen context
                IntPtr deviceContext = GetDC(IntPtr.Zero);
                using (Bitmap capture = new Bitmap(rectangle.Width, rectangle.Height))
                //Get screen context
                using (Graphics deviceGraphics = Graphics.FromHdc(deviceContext))
                //Get graphics from bitmap
                using (Graphics captureGraphics = Graphics.FromImage(capture))
                {
                    // Blit the image data
                    BitBlt(captureGraphics.GetHdc(), 00,
                        640, rectangle.Height, deviceGraphics.GetHdc(),
                       70, rectangle.Top, SRCCOPY);
                    //Save to disk
                    capture.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
                }

            }



Ich hab nun hin und her probiert und auch schon mit StrechBlt probiert aber ich kann das Bild einfach nicht verkleinern (auflösung). Habt ihr nen Tipp?

P.S. www.codeproject.com/...lus/imageresize.aspx hat mir nicht geholfen

Mfg Martin
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Di 11.11.08 17:09 
StretchBlt sollte schon die richtige Richtung sein. Wo drückt denn der Schuh, wie weit bist du schon gekommen?

_________________
>λ=
MartinBush Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 46



BeitragVerfasst: Di 11.11.08 17:42 
Also mein Problem ist das ich ja einen neues Graphic Objekt erstellen muss - daran hänge ich. Also HDC hdcDest.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
BOOL StretchBlt(
  HDC hdcDest,      // handle to destination DC
  int nXOriginDest, // x-coord of destination upper-left corner
  int nYOriginDest, // y-coord of destination upper-left corner
  int nWidthDest,   // width of destination rectangle
  int nHeightDest,  // height of destination rectangle
  HDC hdcSrc,       // handle to source DC
  int nXOriginSrc,  // x-coord of source upper-left corner
  int nYOriginSrc,  // y-coord of source upper-left corner
  int nWidthSrc,    // width of source rectangle
  int nHeightSrc,   // height of source rectangle
  DWORD dwRop       // raster operation code
);
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Di 11.11.08 19:29 
Du kannst doch einfach den BitBlt-Aufruf durch StretchBlt ersetzen und die Parametern anpassen?

_________________
>λ=
MartinBush Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 46



BeitragVerfasst: Mi 12.11.08 12:42 
Anbei mal mein gesamter Code um einen Screenshot zu erstellen und das aufgenommene Bild gleich zu skallieren.

Ich denke das ich IntPtr hdcDest noch erstellen muss oder? Ansonsten ist Quelle ja gleich Ziel. Also wie sollte denn mein Aufruf StretchBlt aussehen?

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
        public class Capturer
        {
            // Declarations
            [DllImport("coredll.dll")]
            public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

            [DllImport("coredll.dll")]
            private static extern IntPtr GetDC(IntPtr hwnd);

            const int SRCCOPY = 0x00CC0020;

            [DllImport("gdi32.dll", SetLastError = true)]
            public static extern int StretchBlt(IntPtr hdcDest, int nXDest, int nYDest,int nWidthDest, int nHeightDest, IntPtr hdcSrc, int nXSrc, int nYSrc, int nWidthSrc, int nHeightSrc,
                uint dwRop);



            public static void Snapshot(string fileName, Rectangle rectangle)
            {
                //Use a zeropointer to get hold of the screen context
                IntPtr deviceContext = GetDC(IntPtr.Zero);
                Bitmap newImage = new Bitmap(newImage.Width, newImage.Height);

                //Get screen context
                using (Graphics deviceGraphics = Graphics.FromHdc(deviceContext))
                //Get graphics from bitmap
                using (Graphics captureGraphics = Graphics.FromImage(capture))
                {
                    // Blit the image data
                  /*  BitBlt(captureGraphics.GetHdc(), 0, 0, 640, rectangle.Height, deviceGraphics.GetHdc(),
                       70, rectangle.Top, SRCCOPY);*/


                        StretchBlt(captureGraphics.GetHdc(), 00200100, deviceGraphics.GetHdc(), 700570480, SRCCOPY);
                    }


                    //Save to disk
                    capture.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
                }

            }
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mi 12.11.08 14:33 
So sollte es stimmen. Was passiert denn?

Edit: Mal abgesehen von der Zeile
ausblenden C#-Quelltext
1:
Bitmap newImage = new Bitmap(newImage.Width, newImage.Height);					

Die gibt so wenig Sinn. Ein anderer Leichtsinnsfehler ist auch noch drin, aber den solltest du selbst finden können.

_________________
>λ=
MartinBush Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 46



BeitragVerfasst: Mi 12.11.08 14:41 
Ja die Zeile mit dem newBitmap muss raus.

Es wirft eine Exeption und das erzeugte Bild ist schlicht schwarz
Chryzler
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Mi 12.11.08 17:16 
Spricht etwas gegen Graphics.CopyFromScreen?
MartinBush Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 46



BeitragVerfasst: Mi 12.11.08 17:37 
Mit Refernez auf : msdn.microsoft.com/e....copyfromscreen.aspx

wüßte ich nicht wie man die gewünschte Auflösung festlegen kann, es geht hier bei meinem Problem nicht um den Ausschnitt der aufgenommen werden soll sondern um die spätere Skalierung.(es soll nichts weggeschnitten werden)

Mfg
Chryzler
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Mi 12.11.08 18:43 
Die Skalierung machst du genau so, wie es in dem von dir verlinkten CodeProject-Artikel erklärt wird.
In Kurzform:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
private void SaveScreenshot(string filename, Size size)
{
    using (Bitmap bitmap = new Bitmap(12801024))
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            graphics.CopyFromScreen(0000, bitmap.Size);
            graphics.DrawImage(bitmap, new Rectangle(Point.Empty, size), new Rectangle(Point.Empty, bitmap.Size), GraphicsUnit.Pixel);
            new Bitmap(bitmap, size).Save(filename);
        }
}

Wie man die Bildschirmauflösung ausliest weiß ich momentan nicht, also sowas natürlich auf keinen Fall hardcoden. ;)