Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to send image data to a server using WebClient

I'm using Webclient to try and send my image I've got on my winform application to a central server. However I've never used WebClient before and I'm pretty sure what I'm doing is wrong.

First of all, I'm storing and displaying my image on my form like so:

_screenCap = new ScreenCapture();
_screenCap.OnUpdateStatus += _screen_CapOnUpdateStatus;
capturedImage = imjObj;
imagePreview.Image = capturedImage;

I've set up an event manager to update my imagePreview image when ever I take a screenshot. Then displaying it when ever the status changes like this:

private void _screen_CapOnUpdateStatus(object sender, ProgressEventArgs e)
{  
  imagePreview.Image = e.CapturedImage;
}

With this image I'm trying to pass it to my server like so:

using (var wc = new WebClient())
{
    wc.UploadData("http://filelocation.com/uploadimage.html", "POST", imagePreview.Image);
 }

I know I should convert the image to a byte[] but I've no idea how to do that. Could someone please point me in the right direction of go about doing this properly?

like image 262
N0xus Avatar asked Mar 23 '23 15:03

N0xus


2 Answers

you can convert to byte[] like this

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
    return  ms.ToArray();
}

if you have path to image, you can also do this

 byte[] bytes = File.ReadAllBytes("imagepath");
like image 182
Ehsan Avatar answered Mar 26 '23 01:03

Ehsan


This may help you...

using(WebClient client = new WebClient())
{
     client.UploadFile(address, filePath);
}

Refered from this.

like image 30
Naren Avatar answered Mar 26 '23 01:03

Naren