WebClient.UploadFile位图?
WebClient.UploadFile a bitmap?
如果这与另一个问题相似,或者问题已经得到解答,我真的很抱歉,但我无法完成这项工作。
Bitmap image = new Bitmap(iw, ih, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(image);
g.CopyFromScreen(ix+left, iy,0,0, new System.Drawing.Size(iw, ih), CopyPixelOperation.SourceCopy);
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Png);
byte[] bitmapBytes = memoryStream.GetBuffer();
string bitmapString = Convert.ToBase64String(bitmapBytes, Base64FormattingOptions.InsertLineBreaks);
try
{
System.Net.WebClient Client = new System.Net.WebClient();
Client.Headers.Add("Content-Type", "binary/octet-stream");
byte[] result = Client.UploadFile("http://localhost/image/index.php", "POST", bitmapString);
string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);
} catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex + " BITMAPSTR: "+bitmapString);
}
我正在截屏,然后我想通过 php 文件将其上传到我的服务器。它应该保存文件,如果我在我的计算机上指定一个已经保存的文件,它就可以工作,但我不能让它与位图一起工作。
if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
move_uploaded_file($tmp_name, "./$name");
}
如何转换位图以使其与 WebClient UploadFile 一起使用?
编辑
抛出错误(翻译,可能不是确切的词):
System.Net.WebException: An exception occured during a WebClient-request. ---> System.ArgumentException: Invalid characters in path.
试试这个。
C#
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Load a bitmap image here
// your code here
// Convert to base64 encoded string
string base64Image = ImageToBase64(myImage, System.Drawing.Imaging.ImageFormat.Jpeg);
// Post image to upload handler
using (WebClient client = new WebClient())
{
byte[] response = client.UploadValues("http://localhost/image/index.php", new NameValueCollection()
{
{ "myImageData", base64Image }
});
Console.WriteLine("Server Said: " + System.Text.Encoding.Default.GetString(response));
}
Console.ReadKey();
}
static System.Drawing.Image GetImage(string filePath)
{
WebClient l_WebClient = new WebClient();
byte[] l_imageBytes = l_WebClient.DownloadData(filePath);
MemoryStream l_stream = new MemoryStream(l_imageBytes);
return Image.FromStream(l_stream);
}
static string ImageToBase64(System.Drawing.Image image, System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
return Convert.ToBase64String(imageBytes);
}
}
}
}
PHP
// Handle Post
if (count($_POST))
{
// Save image to file
$imageData = base64_decode($_POST['myImageData']);
// Write Image to file
$h = fopen('test.jpg', 'w');
fwrite($h, $imageData);
fclose($h);
// Success
exit('Image successfully uploaded.');
}
参考:(我之前的回答之一)
如果这与另一个问题相似,或者问题已经得到解答,我真的很抱歉,但我无法完成这项工作。
Bitmap image = new Bitmap(iw, ih, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(image);
g.CopyFromScreen(ix+left, iy,0,0, new System.Drawing.Size(iw, ih), CopyPixelOperation.SourceCopy);
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Png);
byte[] bitmapBytes = memoryStream.GetBuffer();
string bitmapString = Convert.ToBase64String(bitmapBytes, Base64FormattingOptions.InsertLineBreaks);
try
{
System.Net.WebClient Client = new System.Net.WebClient();
Client.Headers.Add("Content-Type", "binary/octet-stream");
byte[] result = Client.UploadFile("http://localhost/image/index.php", "POST", bitmapString);
string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);
} catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex + " BITMAPSTR: "+bitmapString);
}
我正在截屏,然后我想通过 php 文件将其上传到我的服务器。它应该保存文件,如果我在我的计算机上指定一个已经保存的文件,它就可以工作,但我不能让它与位图一起工作。
if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
move_uploaded_file($tmp_name, "./$name");
}
如何转换位图以使其与 WebClient UploadFile 一起使用?
编辑
抛出错误(翻译,可能不是确切的词):
System.Net.WebException: An exception occured during a WebClient-request. ---> System.ArgumentException: Invalid characters in path.
试试这个。
C#
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Load a bitmap image here
// your code here
// Convert to base64 encoded string
string base64Image = ImageToBase64(myImage, System.Drawing.Imaging.ImageFormat.Jpeg);
// Post image to upload handler
using (WebClient client = new WebClient())
{
byte[] response = client.UploadValues("http://localhost/image/index.php", new NameValueCollection()
{
{ "myImageData", base64Image }
});
Console.WriteLine("Server Said: " + System.Text.Encoding.Default.GetString(response));
}
Console.ReadKey();
}
static System.Drawing.Image GetImage(string filePath)
{
WebClient l_WebClient = new WebClient();
byte[] l_imageBytes = l_WebClient.DownloadData(filePath);
MemoryStream l_stream = new MemoryStream(l_imageBytes);
return Image.FromStream(l_stream);
}
static string ImageToBase64(System.Drawing.Image image, System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
return Convert.ToBase64String(imageBytes);
}
}
}
}
PHP
// Handle Post
if (count($_POST))
{
// Save image to file
$imageData = base64_decode($_POST['myImageData']);
// Write Image to file
$h = fopen('test.jpg', 'w');
fwrite($h, $imageData);
fclose($h);
// Success
exit('Image successfully uploaded.');
}
参考:(我之前的回答之一)