C#图像转换对FILESTREAM [英] C# Convert Image To FileStream
问题描述
我的应用程序至今让用户通过文件选择器中选择一个图像,并上传至通过FTP文件流:
My application so far lets the user select an image via the file selector and upload it through a file stream via FTP:
Stream ftpStream = request.GetRequestStream();
FileStream file = File.OpenRead(fileToUpload);
length = 1024;
buffer = new byte[length];
do
{
bytesRead = file.Read(buffer, 0, length);
ftpStream.Write(buffer, 0, bytesRead);
totalReadBytesCount += bytesRead;
var progress = totalReadBytesCount * 100.0 / totalToUpload;
backgroundWorker1.ReportProgress((int)progress);
}
while (bytesRead != 0);
这工作正常。
所选图像保存为一个文件,然后上传。
This works fine. The selected image is saved as a file and then uploaded.
不过,现在我想将它保存为图像这样我就可以调整它的大小等,所以该代码将是:
Now, however, I wish to save it as a 'Image' so I can resize it etc. So that the code would be:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
image = Image.FromFile(openFileDialog1.FileName);
}
至于反对:
As opposed to:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
file = openFileDialog1.FileName;
}
因此,我的问题是:
My question thus is:
现在我选择的文件是一个图像文件。如何转换入FILESTREAM?如果不能轻松完成,然后我将如何通过FTP上传图片?
先谢谢了。
Now my selected file is a Image file. How do I convert it into the filestream? If this can't be done easily then how would I upload the image via ftp? Thanks in advance.
推荐答案
要写入图像的文件流,我会建议使用的保存功能Image类。即
To write the image to the filestream, I would recommend using the Save function of the Image class. I.E
image = Image.FromFile(openFileDialog1.FileName);
image.Save(ftpStream, System.Drawing.Imaging.ImageFormat.Png);
显然,你需要检查错误,并可能使用不同的图像格式。
Obviously, you'll need to check for errors and possibly use a different image format.
这篇关于C#图像转换对FILESTREAM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!