我通過相機抓取圖片,存放到bitmap變數中,實作直接通過FTP保存成圖片檔案(png)到服務器。
而不用先保存在本地,然后再傳到服務器上。
可不可以,如何實作。
uj5u.com熱心網友回復:
本來就不用保存到本地,你直接Upload到Ftp網路路徑就可以了
uj5u.com熱心網友回復:
你不保存到本地 直接傳輸 不就不保存本地了么, 你之前實作功能了么? ftp 上傳的代碼在哪里uj5u.com熱心網友回復:
ftp上傳代碼有了。現在有bitmap型別變數保存圖片,如何轉換為filestream,不知道
/// <summary>
/// 上傳檔案到FTP服務器
/// </summary>
/// <param name="localFullPath">本地帶有完整路徑的檔案名</param>
/// <param name="updateProgress">報告進度的處理(第一個引數:總大小,第二個引數:當前進度)</param>
/// <returns>是否下載成功</returns>
public static bool FtpUploadFile(string localFullPathName, Action<int, int> updateProgress = null)
{
FtpWebRequest reqFTP;
Stream stream = null;
FileStream fs = null;
try
{
lock(lockobj)
{
FileInfo finfo = new FileInfo(localFullPathName);
if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
{
throw new Exception("ftp上傳目標服務器地址未設定!");
}
Uri uri = new Uri("ftp://" + FtpServerIP + "/" + finfo.Name);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFTP.KeepAlive = false;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用戶,密碼
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;//向服務器發出下載請求命令
reqFTP.ContentLength = finfo.Length;//為request指定上傳檔案的大小
int buffLength = 1024;
byte[] buff = new byte[buffLength];
int contentLen;
fs = finfo.OpenRead();
stream = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
int allbye = (int)finfo.Length;
//更新進度
if (updateProgress != null)
{
updateProgress((int)allbye, 0);//更新進度條
}
int startbye = 0;
while (contentLen != 0)
{
startbye = contentLen + startbye;
stream.Write(buff, 0, contentLen);
//更新進度
if (updateProgress != null)
{
updateProgress((int)allbye, (int)startbye);//更新進度條
}
contentLen = fs.Read(buff, 0, buffLength);
}
stream.Close();
fs.Close();
return true;
}
}
catch (Exception ex)
{
return false;
throw;
}
finally
{
if (fs != null)
{
fs.Close();
}
if (stream != null)
{
stream.Close();
}
}
}
uj5u.com熱心網友回復:
如何改造這個方法,使他第一個引數變為bitmap吶
uj5u.com熱心網友回復:
可以,沒問題。具體原理不多說了,反正原理上是支持的。
我也不想廢啥腦筋給你實作,直接在nuget找個現成的
nuget:FluentFTP
FtpClient client = new FtpClient("123.123.123.123");
client.Upload(你的stream流,"遠端路徑",FtpExists.Overwrite,true,null) ;//具體這些引數我也不想過多的說,你nuget獲取到了,一看就明白
官方:
https://github.com/robinrodricks/FluentFTP
uj5u.com熱心網友回復:
Bitmap bmp = ...;
var request = (FtpWebRequest)WebRequest.Create("ftp://ftp.xxx/xxx/imagexxx.png");
request.Credentials = new NetworkCredential("你的ftp用戶名", "ftp密碼");
request.Method = WebRequestMethods.Ftp.UploadFile;
using (var ftpStream = request.GetRequestStream())
{
bmp.Save(ftpStream, ImageFormat.Png);
}
using (var response = request.GetResponse())
{
}
uj5u.com熱心網友回復:
你不保存到本地 直接傳輸 不就不保存本地了么, 你之前實作功能了么? ftp 上傳的代碼在哪里
ftp上傳代碼有了。現在有bitmap型別變數保存圖片,如何轉換為filestream,不知道
/// <summary>
/// 上傳檔案到FTP服務器
/// </summary>
/// <param name="localFullPath">本地帶有完整路徑的檔案名</param>
/// <param name="updateProgress">報告進度的處理(第一個引數:總大小,第二個引數:當前進度)</param>
/// <returns>是否下載成功</returns>
public static bool FtpUploadFile(string localFullPathName, Action<int, int> updateProgress = null)
{
FtpWebRequest reqFTP;
Stream stream = null;
FileStream fs = null;
try
{
lock(lockobj)
{
FileInfo finfo = new FileInfo(localFullPathName);
if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
{
throw new Exception("ftp上傳目標服務器地址未設定!");
}
Uri uri = new Uri("ftp://" + FtpServerIP + "/" + finfo.Name);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFTP.KeepAlive = false;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用戶,密碼
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;//向服務器發出下載請求命令
reqFTP.ContentLength = finfo.Length;//為request指定上傳檔案的大小
int buffLength = 1024;
byte[] buff = new byte[buffLength];
int contentLen;
fs = finfo.OpenRead();
stream = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
int allbye = (int)finfo.Length;
//更新進度
if (updateProgress != null)
{
updateProgress((int)allbye, 0);//更新進度條
}
int startbye = 0;
while (contentLen != 0)
{
startbye = contentLen + startbye;
stream.Write(buff, 0, contentLen);
//更新進度
if (updateProgress != null)
{
updateProgress((int)allbye, (int)startbye);//更新進度條
}
contentLen = fs.Read(buff, 0, buffLength);
}
stream.Close();
fs.Close();
return true;
}
}
catch (Exception ex)
{
return false;
throw;
}
finally
{
if (fs != null)
{
fs.Close();
}
if (stream != null)
{
stream.Close();
}
}
}
這個其實就可以了。
當然也可以用 HttpWebRequest 和 HttpClient 來做。一樣的,你只需要把流序列化就可以了。
uj5u.com熱心網友回復:
/// <summary>
/// 上傳檔案到FTP服務器
/// </summary>
/// <param name="localFullPath">本地帶有完整路徑的檔案名</param>
/// <param name="updateProgress">報告進度的處理(第一個引數:總大小,第二個引數:當前進度)</param>
/// <returns>是否下載成功</returns>
public static bool FtpUploadFile(Bitmap bmp, Action<int, int> updateProgress = null)
{
FtpWebRequest reqFTP;
Stream stream = null;
FileStream fs = null;
try
{
lock (lockobj)
{
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bmpBytes = ms.GetBuffer();
ms.Close();
if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
{
throw new Exception("ftp上傳目標服務器地址未設定!");
}
Uri uri = new Uri("ftp://" + FtpServerIP + "/" + finfo.Name);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFTP.KeepAlive = false;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用戶,密碼
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;//向服務器發出下載請求命令
reqFTP.ContentLength = bmpBytes.Length;//為request指定上傳檔案的大小
int buffLength = 1024;
stream = reqFTP.GetRequestStream();
int allbye = bmpBytes.Length;
//更新進度
if (updateProgress != null)
{
updateProgress(allbye, 0);//更新進度條
}
int startbye = 0;
var count = allbye / buffLength;
var i = 0;
var bl = true;
while (bl)
{
if (i == count+1)
{
buffLength = allbye % buffLength;
startbye = allbye;
bl = false;
}
else
{
startbye = i * 1024;
}
stream.Write(bmpBytes, startbye, buffLength);
//更新進度
if (updateProgress != null)
{
updateProgress(allbye, startbye);//更新進度條
}
i++;
}
stream.Close();
fs.Close();
return true;
}
}
catch (Exception ex)
{
return false;
throw;
}
finally
{
if (fs != null)
{
fs.Close();
}
if (stream != null)
{
stream.Close();
}
}
}
隨便改了下,沒測驗 出錯的話自己改一改
uj5u.com熱心網友回復:
上傳資源的時候資源本身需要轉成流的
uj5u.com熱心網友回復:
你說的保存檔案后上傳,只是用了filestream,其實還有多種流https://docs.microsoft.com/en-us/dotnet/api/system.io.stream?view=netframework-4.8
uj5u.com熱心網友回復:
/// <summary>
/// 上傳檔案到FTP服務器
/// </summary>
/// <param name="localFullPath">本地帶有完整路徑的檔案名</param>
/// <param name="updateProgress">報告進度的處理(第一個引數:總大小,第二個引數:當前進度)</param>
/// <returns>是否下載成功</returns>
public static bool FtpUploadFile(Bitmap bmp, Action<int, int> updateProgress = null)
{
FtpWebRequest reqFTP;
Stream stream = null;
FileStream fs = null;
try
{
lock (lockobj)
{
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bmpBytes = ms.GetBuffer();
ms.Close();
if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
{
throw new Exception("ftp上傳目標服務器地址未設定!");
}
Uri uri = new Uri("ftp://" + FtpServerIP + "/" + finfo.Name);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFTP.KeepAlive = false;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用戶,密碼
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;//向服務器發出下載請求命令
reqFTP.ContentLength = bmpBytes.Length;//為request指定上傳檔案的大小
int buffLength = 1024;
stream = reqFTP.GetRequestStream();
int allbye = bmpBytes.Length;
//更新進度
if (updateProgress != null)
{
updateProgress(allbye, 0);//更新進度條
}
int startbye = 0;
var count = allbye / buffLength;
var i = 0;
var bl = true;
while (bl)
{
if (i == count+1)
{
buffLength = allbye % buffLength;
startbye = allbye;
bl = false;
}
else
{
startbye = i * 1024;
}
stream.Write(bmpBytes, startbye, buffLength);
//更新進度
if (updateProgress != null)
{
updateProgress(allbye, startbye);//更新進度條
}
i++;
}
stream.Close();
fs.Close();
return true;
}
}
catch (Exception ex)
{
return false;
throw;
}
finally
{
if (fs != null)
{
fs.Close();
}
if (stream != null)
{
stream.Close();
}
}
}
隨便改了下,沒測驗 出錯的話自己改一改
bitmap.save(ms,ImageFormat.Png)后,12M的圖片變成了8M多,是什么原因,我需要原質量保存圖片。
uj5u.com熱心網友回復:

圖片寬度和高度一樣,為什么檔案大小不同。
uj5u.com熱心網友回復:
不同格式的圖片,保存大小是不同的。uj5u.com熱心網友回復:
不同格式的圖片,保存大小是不同的。
但都是png啊
uj5u.com熱心網友回復:
不同格式的圖片,保存大小是不同的。
但都是png啊
就是用了bitmap.save(ms,ImageFormat.Png)
uj5u.com熱心網友回復:
ftp上傳后默認格式是ASCII,不是binary,需要你設定成binary具體要看你用哪種方式上傳了,應該都有這個選項的
uj5u.com熱心網友回復:
你好,你在解決了嗎,可以發代碼看看嗎轉載請註明出處,本文鏈接:https://www.uj5u.com/net/17964.html
標籤:C#
下一篇:C# 資料庫自增的列
