我写的图片、文件操作类(自己写的,以后用到的时候可以直接用,哈哈哈)

public partial class ImageHelper
{
    public static string GetConfiguration(string key)
    {
        return ConfigurationManager.AppSettings[key];
    }
    public static string ImageServer
    {
        get { return GetConfiguration("ImageServer"); }
    }
    public static string GetUrl(string plucode)
    {
        try
        {
            string href;
            if (plucode.Length < 7)
            {
                   href = String.Format("{0}/{1}/{2}/{3}.jpg", ImageServer, "0000000",
                   "00", plucode);
            }
else if (plucode.Length < 9)
{
href = String.Format("{0}/{1}/{2}/{3}.jpg", ImageServer, plucode.Substring(0, 7),
"00", plucode);
}
else
{
href = String.Format("{0}/{1}/{2}/{3}.jpg", ImageServer, plucode.Substring(0, 7),
plucode.Substring(7, 2), plucode);
}

return href;
}
catch (Exception exception)
{
return string.Empty;
}
}

public static string UpLoad(string plucode)
{
     var openFile = new OpenFileDialog();
     var imageUrl = "";
     if (openFile.ShowDialog() == DialogResult.OK)
     {
         var picpath = openFile.FileName;
         var picname = openFile.SafeFileName;
         var picStream = new FileStream(picpath, FileMode.Open);
         var bytes = GetBytes(picStream);

         var webUpFile = new UpFileContent.webUpFileServiceSoapClient();
         imageUrl = webUpFile.UploadFileByImg(bytes, picname, plucode);
     }
     return imageUrl;
}

public static byte[] GetBytes(Stream stream)
{
     var bytes = new byte[stream.Length];
     stream.Read(bytes, 0, bytes.Length);
     stream.Seek(0, SeekOrigin.Begin);
     return bytes;
}

public static Image GetImage(string url)
{
    try
    {
         var webRequest = WebRequest.Create(url);
         var webResponse = webRequest.GetResponse();

         var stream = webResponse.GetResponseStream();
         var image = Image.FromStream(stream);
         return image;
     }catch(Exception exception)
     {
         return null;
     }
  }
}

 

posted @ 2013-03-16 15:36  dotNet修行之路  阅读(306)  评论(1编辑  收藏  举报