0

I am working on a C# project that captures jpeg image from a camera, convert it to BMP on stream, rename and publish it to SFTP as an image file. I am using Renci.SSHNET library that handles SFTP part.

I am struck converting the memory stream to an image file before posting it to SFTP. Is there a way that solves my problem? Thanks in Advance.

PS - I prefer not to store the file to local and re-process it to SFTP.

  • It depends on what SFTP client you use. Here is an example for Renci.SSHNet https://stackoverflow.com/questions/45945512/upload-data-from-memory-to-sftp-server-using-ssh-net – opewix Jan 18 '19 at 14:33

1 Answers1

0

Bitmap has an overload for Save that writes to a MemoryStream so this should work:

using (var sftp = new SftpClient("localhost", "a","b"))
{
    sftp.Connect();

    using (MemoryStream ms = new MemoryStream())
    {
        Bitmap bmp = new Bitmap(10,10);
        bmp.Save(ms,ImageFormat.Bmp);

        ms.Seek(0, SeekOrigin.Begin);

        sftp.UploadFile(ms,"File.bmp");

    }
}
Thomas N
  • 623
  • 1
  • 4
  • 14
  • This works. Thanks Thomas! – Karthikeyyan S Jan 18 '19 at 14:58
  • I do not see how this answers the question. @KarthikeyyanS You wrote that you are *"struck **converting the memory stream to an image** file before posting it to SFTP."* - So it looks like you already have memory stream - so there's nothing to convert. While this answer shows an exact opposite of what you have asked for - **converting image to memory stream**. – Martin Prikryl Jan 18 '19 at 15:40
  • @MartinPrikryl- Yes, I already have a memory stream but I was clue-less on how to upload it as a file to SFTP. But last two lines of Thomas code was what I was looking for. I am posting a code using FileStream that drove me crazy. using (FileStream fs = new FileStream(sourcefile, FileMode.Open)) { client.BufferSize = 4 * 1024; client.UploadFile(fs, Path.GetFileName(sourcefile)); } – Karthikeyyan S Jan 18 '19 at 17:00