0

Can I upload print screen to FTP without saving it on my drive?

At the current state I save the print screen to drive "E:\" and then upload to FTP.

Saving the image:

 Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
 Screen.PrimaryScreen.Bounds.Height);
 Graphics graphics = Graphics.FromImage(bitmap as Image);
 graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
 bitmap.Save(@"E:\pic.jpg", ImageFormat.Jpeg);

Upload to FTP:

using (var client = new WebClient())
 {
client.Credentials = new NetworkCredential("username", "password");
client.UploadFile("ftp://127.0.0.1/xy.jpg", WebRequestMethods.Ftp.UploadFile, @"E:\pic.jpg");
 }
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Nathan J
  • 89
  • 7

1 Answers1

1

You can save the Bitmap to MemoryStream, load the result into a byte array, and write the byte array to the WebRequest stream

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://127.0.0.1/xy.jpg");
request.Method = WebRequestMethods.Ftp.UploadFile;


request.Credentials = new NetworkCredential("username", "password");

byte[] fileContents;
using (MemoryStream sourceStream = new MemoryStream())
{
    Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
        Screen.PrimaryScreen.Bounds.Height);
    Graphics graphics = Graphics.FromImage(bitmap as Image);
    graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
    bitmap.Save(sourceStream, ImageFormat.Jpeg);
    fileContents = sourceStream.ToArray();
}

request.ContentLength = fileContents.Length;

using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(fileContents, 0, fileContents.Length);
}

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
    Console.WriteLine($"Upload File Complete, status {response.StatusDescription}");
}
Martheen
  • 5,198
  • 4
  • 32
  • 55
  • It *might* be possible to just bitmap.Save(requestStream,ImageFormat.Jpeg), skipping the MemoryStream and fileContents, but I haven't tried it. Feel free to try and edit this answer if that works. – Martheen Apr 17 '20 at 23:47
  • Definitely, `bitmap.Save(requestStream,ImageFormat.Jpeg)` looks like a more efficient way. This code copies of the data twice over. I rare case it work work (if `Bitmap` needs seekable stream), you can at least avoid the `byte[]` array, by using `sourceStream.Position = 0; sourceStream.CopyTo(requestStream);`. – Martin Prikryl Apr 20 '20 at 07:27
  • @Martheen, Martin Prikryl Thank you guys for taking the time to help, I have 2 more questions (if you don't mind) 1, Why the application is lagging for x seconds while taking or uploading the picture ( I don't know which causing the lagg) 2, How hard is it for another program to manipulate this screen taking method? (It will be great if you can give an example code so I can try it and I'd see more of it.) Thanks. – Nathan J Apr 21 '20 at 11:26
  • Create a new question for those, but only after you try the direct saving to stream as pointed by @Martin, and log the time spent by each part of the function https://stackoverflow.com/questions/14019510/calculate-the-execution-time-of-a-method – Martheen Apr 21 '20 at 11:54
  • I'd try it if I could know which parts needed to be changed in answer's code. I see I can avoid the byte[] array but where I need to place these: `sourceStream.Position = 0; sourceStream.CopyTo(requestStream);` ? – Nathan J Apr 21 '20 at 12:09
  • Put those **instead** the `requestStream.Write(fileContents, 0, fileContents.Length);`. + Remove the `request.ContentLength = fileContents.Length;`, that has no effect in FTP anyway. – Martin Prikryl Apr 21 '20 at 12:31
  • I'm not sure I did it in the right way. I updated the question, can you take a look below --- updated code --- please? The form still freezing and I can't do nothing on the form for a few seconds while this code running. – Nathan J Apr 21 '20 at 13:40
  • Remove all the lines with `fileContents`, they are useless. + For the freezing problem, post a new question (and do not forget to include how the code is linked to the form). – Martin Prikryl Apr 21 '20 at 13:45
  • Ok, thanks for your help! I figured out... my FTP upload method causing the freezing problem. I'll post a new question with those code. – Nathan J Apr 21 '20 at 13:55
  • https://stackoverflow.com/questions/61345576/c-sharp-ftp-directory-create-upload-causing-form-freeze Can you take a look at this please? – Nathan J Apr 21 '20 at 14:16