图片压缩以及上传的问题

jhdxhj 2008-11-08 08:44:49
如何通过程序把图片压缩然后通过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上传?
...全文
270 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mjjzg 2008-11-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cpio 的回复:]
两个方法都有了,把压缩后的图片上传就行了啊

MakeThumbnailMap(参数).Save(文件名);

再调用
FtpUpFile1(文件名)

它不是静态的,需要实例化
[/Quote]
同意
cpio 2008-11-08
  • 打赏
  • 举报
回复
两个方法都有了,把压缩后的图片上传就行了啊

MakeThumbnailMap(参数).Save(文件名);

再调用
FtpUpFile1(文件名)

它不是静态的,需要实例化
twtetg 2008-11-08
  • 打赏
  • 举报
回复
没做过,帮你顶起!
zx005 2008-11-08
  • 打赏
  • 举报
回复
Mark
accomp 2008-11-08
  • 打赏
  • 举报
回复
既然都可以运行了 ,那就保存下
以后好用
格拉 2008-11-08
  • 打赏
  • 举报
回复
接分
jhdxhj 2008-11-08
  • 打赏
  • 举报
回复
当然可以运行,这个问题目前已经解决,答案是上面的人帮我回答的
易之盛软件 2008-11-08
  • 打赏
  • 举报
回复
同学,你那个图片的压缩代码能在客户端运行吗?

我想楼主的意思是在客户端将图片压缩后在进行上传吧
shanying_0 2008-11-08
  • 打赏
  • 举报
回复
恩。你都有方法了。依次调用就可以了。
teemai 2008-11-08
  • 打赏
  • 举报
回复
帮顶

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧