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?
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");
This may help you...
using(WebClient client = new WebClient())
{
client.UploadFile(address, filePath);
}
Refered from this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With