Saturday, 31 August 2013

Upload image to server from PictureBox

Upload image to server from PictureBox

I have one pictureBox and a button on form1 one. When clicked on button it
should upload the file to server. For now I am using below method first
save the image locally and then upload to server:
Bitmap bmp = new Bitmap(this.form1.pictureBox1.Width,
this.form1.pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
Rectangle rect =
this.form1.pictureBox1.RectangleToScreen(this.form1.pictureBox1.ClientRectangle);
g.CopyFromScreen(rect.Location, Point.Empty, this.form1.pictureBox1.Size);
g.Dispose();
bmp.Save("filename", ImageFormat.Jpeg);
And then uploading that file:
using (var f = System.IO.File.OpenRead(@"F:\filename.jpg"))
{
HttpClient client = new HttpClient();
var content = new StreamContent(f);
var mpcontent = new MultipartFormDataContent();
content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
mpcontent.Add(content);
client.PostAsync("http://domain.com/upload.php", mpcontent);
}
I cant use Bitmap type in StreamContent. How can I stream the image from
pictureBox directly instead saving it as file first? Thanks

No comments:

Post a Comment