.net上传图片实例

Posted 拍空格

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.net上传图片实例相关的知识,希望对你有一定的参考价值。

前台代码:
<asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button2" runat="server" Text="Butto2n" OnClick="Button2_Click" />
后台代码:
protected void Button2_Click(object sender, EventArgs e)
{
  if (FileUpload1.HasFiles) {
    byte[] pbyte = FileUpload1.FileBytes;
    string type = FileUpload1.PostedFile.ContentType;
    //验证输入图像尺寸
    byte
[] newPbyte = ValidatePicture(pbyte, type);
    //保存图片 第一个参数为图片名字,如果图片存在数据里面那么就可以以图片id来指定命名如101.jpeg
    SavePictureInImageServer(
101, newPbyte, type); } }

public void SavePictureInImageServer(int pictureId, byte[] pictureBinary, string mimeType)
{
  string[] parts = mimeType.Split(‘/‘);
  string lastPart = parts[parts.Length - 1];
  switch (lastPart)
  {
    case "pjpeg":
    lastPart = "jpg";
    break;
    case "x-png":
    lastPart = "png";
    break;
    case "x-icon":
    lastPart = "ico";
    break;
  }

//图片名称
string localFilename = string.Format("{0}.{1}", pictureId.ToString(), lastPart);
//创建文件 保存至存储路径下
File.WriteAllBytes(Path.Combine(LocalImagePath, localFilename), pictureBinary);
}


//获取图片存储路径

public string LocalImagePath
{

  get
  {
    string path = HttpContext.Current.Request.PhysicalApplicationPath + "images";
    return path;
  }
}


//验证输入图像尺寸

public byte[] ValidatePicture(byte[] pictureBinary, string mimeType)
{
  using (MemoryStream stream = new MemoryStream(pictureBinary))
  {
    var b = new Bitmap(stream);
    int maxSize = 600;//此处设置好图片最大尺寸

    if ((b.Height > maxSize) || (b.Width > maxSize))
    {

      //计算图像尺寸,同时保持纵横
      var newSize = CalculateDimensions(b.Size, maxSize);
      var newBitMap = new Bitmap(newSize.Width, newSize.Height);
      var g = Graphics.FromImage(newBitMap);
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
      g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
      g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
      g.DrawImage(b, 0, 0, newSize.Width, newSize.Height);

      var m = new MemoryStream();
      var ep = new EncoderParameters();
      ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); //100L-图片清晰度
      ImageCodecInfo ici = GetImageCodecInfoFromMimeType(mimeType);//返回指定类型的第一个影像编解码器信息。
      if (ici == null)
      ici = GetImageCodecInfoFromMimeType("image/jpeg");
      newBitMap.Save(m, ici, ep);
      newBitMap.Dispose();
      b.Dispose();

      return m.GetBuffer();

    }
    else
    {
      b.Dispose();
      return pictureBinary;
    }
  }
}

//计算图像尺寸,同时保持纵横

private Size CalculateDimensions(Size originalSize, int targetSize)
{
  var newSize = new Size();
  if (originalSize.Height > originalSize.Width) // portrait
  {
    newSize.Width = (int)(originalSize.Width * (float)(targetSize / (float)originalSize.Height));
    newSize.Height = targetSize;
  }
  else
  {
    newSize.Height = (int)(originalSize.Height * (float)(targetSize / (float)originalSize.Width));
    newSize.Width = targetSize;
  }
  return newSize;
}


//返回指定类型的第一个影像编解码器信息。

private ImageCodecInfo GetImageCodecInfoFromMimeType(string mimeType)
{
  var info = ImageCodecInfo.GetImageEncoders();
  foreach (var ici in info)
  if (ici.MimeType.Equals(mimeType, StringComparison.OrdinalIgnoreCase))
  return ici;
  return null;
}

 

 

<pre></pre>
<p>&nbsp;</p>
<p>北清论文网-中国领先的应届毕业生论文网站</p>
<p>【<a href="http://www.bqlww.com/">北清论文网</a>】【<a href="http://www.51byl.net/">我要毕业啦论文网</a>】</p>

以上是关于.net上传图片实例的主要内容,如果未能解决你的问题,请参考以下文章

asp.net mvc之图片上传,webuploader多实例上传疑问,在这里轻松解决!

asp.net mvc之图片上传,webuploader多实例上传疑问,在这里轻松解决!

android中如何上传图片到FTP服务器

thinkphp 上传图片未选择文件

asp.net core 通过ajax上传图片及wangEditor图片上传

asp.net上传图片