winform实现为web项目上传图片的功能
项目中有个需求:用winform维护web服务器端的数据,其中需要添加图片,且图片上传需要考虑手机端,要将图片压缩后存在另个文件夹下由手机端调用,以此减轻手机端的数据量过大的负担。
实现思路如下:1.用webservice作为winform的数据层对web服务器数据进行维护;2.webservice中添加如下代码由winform调用:
#region---上传图片---
/// <summary>
///
/// </summary>
/// <param name="content"></param>
/// <param name="pathandname"></param>
/// <returns></returns>
[WebMethod]
public int UpLoadFile(byte[] content, string pathandname)
{
int result = -2;//-2,未知错误,-1上传失败,1web图片上传成功但手机图片未成功上传,2web和手机图片都上传成功。
try
{
string phonePicturePath = ConfigurationManager.AppSettings["phonePicturePath"];
int percentage = Convert.ToInt32(ConfigurationManager.AppSettings["percentage"]);
int index = pathandname.LastIndexOf(".");
if (index == 0)
{
result = -1;
}
else
{
string extended = string.Empty;
if (index + 1 == pathandname.Length)
{
result = -1;
}
else
{
extended = pathandname.Substring(index + 1);
if (extended == "jpeg" || extended == "gif" || extended == "jpg" || extended == "png")
{
try
{
File.WriteAllBytes(Server.MapPath(pathandname), content);//web上传成功
result = 1;
phonePicturePath += pathandname.Substring(pathandname.LastIndexOf('/'));
if (GetPicThumbnail(pathandname, phonePicturePath, percentage))//全部上传成功
{
result = 2;
}
}
catch (Exception ex)
{
result = -1;
}
}
else
{
result = -1;
}
}
}
}
catch (Exception ex)
{
result = -1;
}
return result;
}
/// <summary>
/// 压缩图片
/// </summary>
/// <param name="sFile">文件路径</param>
/// <param name="outPath">存放路径</param>
/// <param name="flag">压缩比率</param>
/// <returns>Boolean</returns>
[WebMethod]
public bool GetPicThumbnail(string sFile, string outPath, int flag)
{
System.Drawing.Image iSource = System.Drawing.Image.FromFile(Server.MapPath(sFile));
ImageFormat tFormat = iSource.RawFormat;
//以下代码为保存图片时,设置压缩质量
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//设置压缩的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
补充:Web开发 , ASP.Net ,