C# 以png格式将图像从位图上载到服务器
我正在尝试将C#winform应用程序中的C# 以png格式将图像从位图上载到服务器,c#,winforms,png,C#,Winforms,Png,我正在尝试将C#winform应用程序中的System.Drawing.Bitmap变量作为.png图像上传到服务器上。现在我知道如何将位图保存为.png文件,然后将其上载到我的服务器 //Save bitmap variable as .png bitmap.Save("img_1.png", ImageFormat.Png); 然后像这样上传到我的服务器上: using (WebClient client = new WebClient()) { client.Credential
System.Drawing.Bitmap
变量作为.png
图像上传到服务器上。现在我知道如何将位图
保存为.png
文件,然后将其上载到我的服务器
//Save bitmap variable as .png
bitmap.Save("img_1.png", ImageFormat.Png);
然后像这样上传到我的服务器上:
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("FTP_username",
"FTP_password");
client.UploadFile("ftp://100.100.100.100/new_folder/img_1.png",
"img_1.png");
}
如何将Bitmap Bitmap
变量作为.png
文件直接上载到服务器上,而不必先在本地将其保存为新的.png
文件?尝试以下方法:
using System.Drawing;
Bitmap b = (Bitmap)Bitmap.FromStream(file.InputStream);
using (MemoryStream ms = new MemoryStream()) {
b.Save(ms, ImageFormat.Png);
// use the memory stream to base64 encode..
}
您可以将
位图
保存到流并上载流:
using (WebClient client = new WebClient())
using (var ms = new MemoryStream())
{
bitmap.Save(ms, ImageFormat.Png);
client.Credentials = new NetworkCredential("FTP_username", "FTP_password");
client.UploadData("ftp://100.100.100.100/new_folder/img_1.png", ms.ToArray());
}
由于方法是
UploadFile
,因此不能将文件的URL作为字符串
作为第二个参数
我的建议是,如果用户不必看到PNG文件,他们就不会看到PNG文件
%TEMP%\RandString\ImageName.jpg
%TEMP%\RandString\
看来这是可能的。我用@donboitnott answer实现了这一点