Reputation: 2724
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?
Upvotes: 4
Views: 6732
Reputation: 2738
You need to set the ContentType
header to that of image/gif
or possibly binary/octet-stream
and call GetBytes()
on the image.
using (var wc = new WebClient { UseDefaultCredentials = true })
{
wc.Headers.Add(HttpRequestHeader.ContentType, "image/gif");
//wc.Headers.Add("Content-Type", "binary/octet-stream");
wc.UploadData("http://filelocation.com/uploadimage.html",
"POST",
Encoding.UTF8.GetBytes(imagePreview.Image));
}
Upvotes: 0
Reputation: 2311
This may help you...
using(WebClient client = new WebClient())
{
client.UploadFile(address, filePath);
}
Refered from this.
Upvotes: 3
Reputation: 32681
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");
Upvotes: 4