0

I don't know if my way to get a screenshot is completly wrong, but I need to get the data in kind of a format to sent it to a php script. The php script should build an image from this data (the screenshot). I use this code:

         internal static byte[] ImageToByteArray(Image img)
    {
        byte[] byteArray = new byte[0];
        MemoryStream stream = new MemoryStream();
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        stream.Close();
        byteArray = stream.ToArray();
        return byteArray;
    }

   public static string TakeScreenshot()
       {

           String filepath = @"C:\log2.txt";

           Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

           Graphics graphics = Graphics.FromImage(bitmap);

           graphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
           graphics.Dispose();
           //just for debug
           bitmap.Save("C:\\temp.png");


           Image img = (Image)bitmap;
           string str = System.Text.Encoding.Default.GetString(ImageToByteArray(img));
           System.IO.File.AppendAllText(filepath, str);
           return "OH";
       }

I use this function to get an byte array from the image object: stackoverflow post

I tried to rename my "log2.txt" to "log2.png" but it doesn't work at all. Any idea how to get the data in kind of a format a php script can easily convert to a viewable image?

Community
  • 1
  • 1
Horray
  • 1
  • 1
    Why are you trying to save this out to a text file at all? Why not just pass the image as it is? – Nanhydrin Nov 14 '14 at 12:01
  • I need to sent the image as an byte array or as an string to the php script. – Horray Nov 14 '14 at 12:06
  • It depends on how you read and send the file. You cannot use `System.Text.Encoding.Default.GetString()` and `AppendAllText()` to write binary data as text. You should just use `C:\\Temp.png` in your sending function. – CodeCaster Nov 14 '14 at 12:09
  • I would need to pass the data as string or byte array to the PHP script, so just my use of `System.Text.Encoding.Default.GetString()` destroys the data structure. Data I would get by using `ImageToByteArray(img)` should be formated right to be sent to PHP script? – Horray Nov 14 '14 at 12:12
  • No, again, that all depends on how you want to send it to the PHP script and what that script expects. – CodeCaster Nov 14 '14 at 12:13
  • How exactly are you passing the data to the PHP script, and as CodeCaster asked what exactly is the PHP script expecting? It might be worth including that script here as well. – Nanhydrin Nov 14 '14 at 13:10

0 Answers0