图片压缩以及上传的问题
如何通过程序把图片压缩然后通过FTP进行上传?求高人给解决办法
我这里有图片压缩的程序:
public static Image MakeThumbnailMap(Image img, int nWidth, int nHeight, string strMode)
{
int nTowidth = nWidth;
int nToheight = nHeight;
int nX = 0;
int nY = 0;
int nOw = img.Width;
int nOh = img.Height;
switch (strMode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
nToheight = img.Height * nWidth / img.Width;
break;
case "H"://指定高,宽按比例
nTowidth = img.Width * nHeight / img.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)img.Width / (double)img.Height > (double)nTowidth / (double)nToheight)
{
nOh = img.Height;
nOw = img.Height * nTowidth / nToheight;
nY = 0;
nX = (img.Width - nOw) / 2;
}
else
{
nOw = img.Width;
nOh = img.Width * nHeight / nTowidth;
nX = 0;
nY = (img.Height - nOh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image bitMap = new System.Drawing.Bitmap(nTowidth, nToheight);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitMap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(img, new System.Drawing.Rectangle(0, 0, nTowidth, nToheight),
new System.Drawing.Rectangle(nX, nY, nOw, nOh),
System.Drawing.GraphicsUnit.Pixel);
return bitMap;
}
FTP图片上传的程序:其中strFileName是文件选择框中选择的图片名称
private bool FtpUpFile1(string strFileName)
{
try
{
FileInfo fileInf = new FileInfo(strFileName);
FtpWebRequest reqFTP;
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(MainForm.m_sTravelSet + fileInf.Name));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(MainForm.m_sTravelSetFtpUser, MainForm.m_sTravelSetFtpPassword);
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
// 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length;
// 缓冲大小设置为2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength);
int allbye = (int)fileInf.Length;
int startbye = 0;
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
startbye += contentLen;
}
// 关闭两个流
strm.Close();
fs.Close();
return true;
}
catch
{
return false;
}
}
请问如何把这2段程序整合起来实现图片先压缩然后再通过FTP上传?