C#上传图到SQL数据库中

本文介绍了一种图片上传至数据库的方法,包括图片的按比例缩放和生成缩略图的功能。通过对图片尺寸的判断来决定其缩放的方式,确保图片大小适合数据库存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上传图到数据库,如果上传的图过大,可以按比例缩放。
还可生成小的缩略图保存到数据库中。

 

 using System.IO;
 using System.Data.SqlClient;
 using System.Configuration;

private System.Drawing.Imaging.ImageFormat GetImageType(object strContentType)
  {
   if ((strContentType.ToString().ToLower()) == "image/pjpeg")
   {
    return System.Drawing.Imaging.ImageFormat.Jpeg;
   }
   else if ((strContentType.ToString().ToLower()) == "image/gif")
   {
    return System.Drawing.Imaging.ImageFormat.Gif;
   }
   else if ((strContentType.ToString().ToLower()) == "image/bmp")
   {
    return System.Drawing.Imaging.ImageFormat.Bmp;
   }
   else if ((strContentType.ToString().ToLower()) == "image/tiff")
   {
    return System.Drawing.Imaging.ImageFormat.Tiff;
   }
   else if ((strContentType.ToString().ToLower()) == "image/x-icon")
   {
    return System.Drawing.Imaging.ImageFormat.Icon;
   }
   else if ((strContentType.ToString().ToLower()) == "image/x-png")
   {
    return System.Drawing.Imaging.ImageFormat.Png;
   }
   else if ((strContentType.ToString().ToLower()) == "image/x-emf")
   {
    return System.Drawing.Imaging.ImageFormat.Emf;
   }
   else if ((strContentType.ToString().ToLower()) == "image/x-exif")
   {
    return System.Drawing.Imaging.ImageFormat.Exif;
   }
   else if ((strContentType.ToString().ToLower()) == "image/x-wmf")
   {
    return System.Drawing.Imaging.ImageFormat.Wmf;
   }
   else
   {
    return System.Drawing.Imaging.ImageFormat.MemoryBmp;
   }
  }

 

  private byte[] imageToStream(int nWidth ,int nHeigth,Stream fileDataStream,string fileType)
  {

//这个对于大图效果就不好了。
  
   //把image转成stream
   System.Drawing.Image image;
   image = System.Drawing.Image.FromStream(fileDataStream);
   image = image.GetThumbnailImage(nWidth, nHeigth, null, IntPtr.Zero);

   Stream MemStream = new MemoryStream();
   image.Save(MemStream,GetImageType(fileType));
   //MemStream.WriteTo(Response.OutputStream);

   int fileLength = Convert.ToInt32(MemStream.Length);
   
   byte[] fileData =new byte[fileLength];

   //把文件流填充到数组
   MemStream.Position = 0;
   MemStream.Read(fileData,0,fileLength);
   MemStream.Close();

   return  fileData;

  }

private byte[] imageToStreamThumb(int nWidth, int nHeigth, System.Drawing.Image bmp, string fileType)
  {

//用这个话,大图变为小图,效果会好很多
   Stream MemStream = new MemoryStream();
   
   System.Drawing.Image bmpSave = new Bitmap(nWidth, nHeigth);
   Graphics graphics = Graphics.FromImage(bmpSave);
   graphics.SmoothingMode =  System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
   graphics.DrawImage(bmp, new Rectangle(0, 0, nWidth, nHeigth), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
   //bmpSave.Save(TempFileName, ImageFormat.Jpeg);
   bmpSave.Save(MemStream, System.Drawing.Imaging.ImageFormat.Jpeg);
   bmp.Dispose();

   int fileLength = Convert.ToInt32(MemStream.Length);
   
   byte[] fileData =new byte[fileLength];

   //把文件流填充到数组
   MemStream.Position = 0;
   MemStream.Read(fileData,0,fileLength);
   MemStream.Close();

   return  fileData;

  }

 

 

 private void btuploadPIC_Click(object sender, System.EventArgs e)
  {
   //得到文件大小
   int fileLength = uploadFile.PostedFile.ContentLength;

   //得到提交的文件
   Stream fileDataStream = uploadFile.PostedFile.InputStream;

   //创建数组
   byte[] fileData = new byte[fileLength];
   //创建缩略图的数组
   byte[] fileDataThumb = new byte[1];

   //把文件流填充到数组
   fileDataStream.Read(fileData,0,fileLength);

   //得到文件路径
   string fileTitle = uploadFile.PostedFile.FileName.ToString();

   //得到文件名
   string strFileName = uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('//')+1);


   //得到文件类型
   string fileType = uploadFile.PostedFile.ContentType;

//   //得到上传图片的宽和高
//   System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(fileTitle));
   System.Drawing.Image image = new Bitmap(fileTitle);

   int nWidth = image.Width;
   int nHeight = image.Height;

   //如果高大于宽,说明图是竖的.
   if (nWidth < nHeight )
   {
    //大于4:3的比例应该变宽
    if (nHeight/nWidth >= 4/3)
    {
     //宽大于800
     if (nHeight > 800 )
     {
      //按比例,      
      nWidth = (int)(800*nWidth/nHeight);
      nHeight = 800;
      //fileData = imageToStream(nWidth,800,fileDataStream,fileType);
      //fileDataThumb = imageToStream((int)80*nWidth/800,80,fileDataStream,fileType);

      fileData = imageToStreamThumb(nWidth,800,image,fileType);
      fileDataThumb = imageToStreamThumb((int)80*nWidth/800,80,image,fileType);
      
     }
     else
     {
      //fileData = imageToStreamThumb((int)nHeight/10,(int)nWidth/10,fileDataStream,fileType);
      fileDataThumb = imageToStreamThumb((int)nHeight/10,(int)nWidth/10,image,fileType);

     }
    }
     //小于4:3的比例应该变高
    else
    {
     //高大于600
     if (nWidth > 600 )
     {     
      //按比例
      nHeight = (int)(600*nHeight/nWidth);
      nWidth = 600;
      //fileData = imageToStream(600,nHeight,fileDataStream,fileType);
      //fileDataThumb = imageToStream(60,(int)60*nHeight/600,fileDataStream,fileType);

      fileData = imageToStreamThumb(600,nHeight,image,fileType);
      fileDataThumb = imageToStreamThumb(60,(int)60*nHeight/600,image,fileType);

     }
     else
     {
      //fileDataThumb = imageToStream((int)nHeight/10,(int)nWidth/10,fileDataStream,fileType);
      fileDataThumb = imageToStreamThumb((int)nHeight/10,(int)nWidth/10,image,fileType);
     }

    }
   }
   //如果高小于宽,说明图是横的.
   else
   {
    //大于4:3的比例应该变宽
    if (nWidth/nHeight >= 4/3)
    {
     //宽大于800
     if (nWidth > 800 )
     {
      //按比例,
      nHeight = (int)(800*nHeight/nWidth);
      nWidth = 800;
      //fileData = imageToStream(800,nHeight,fileDataStream,fileType);
      //fileDataThumb = imageToStream(80,(int)80*nHeight/800,fileDataStream,fileType);

      fileData = imageToStreamThumb(800,nHeight,image,fileType);
      fileDataThumb = imageToStreamThumb(80,(int)80*nHeight/800,image,fileType);

    }
     else
     {     
      //fileDataThumb = imageToStream((int)nWidth/10,(int)nHeight/10,fileDataStream,fileType);
      fileDataThumb = imageToStreamThumb((int)nWidth/10,(int)nHeight/10,image,fileType);
     }
    }
     //小于4:3的比例应该变高
    else
    {
     //高大于600
     if (nHeight > 600 )
     {     
      //按比例
      nWidth = (int)(600*nWidth/nHeight);
      nHeight = 600;
      //fileData = imageToStream(nWidth,600,fileDataStream,fileType);
      //fileDataThumb = imageToStream((int)60*nWidth/600,60,fileDataStream,fileType);

      fileData = imageToStreamThumb(nWidth,600,image,fileType);
      fileDataThumb = imageToStreamThumb((int)60*nWidth/600,60,image,fileType);

     }
     else
     {
      //fileDataThumb = imageToStream((int)nWidth/10,(int)nHeight/10,fileDataStream,fileType);
      fileDataThumb = imageToStreamThumb((int)nWidth/10,(int)nHeight/10,image,fileType);   
     
     }
    }
   }
   
   //   //数据集
   //   DataSet objDataSet;
   //   //自己生成的类
   //   DataOperation objDataOperation;
   //   //数据库连接串
   string strConnection;
   //查询字符串
   string strSQL;

   strConnection = ConfigurationSettings.AppSettings["Tea"];
   SqlConnection objConnection=new SqlConnection (strConnection);
   strSQL="INSERT INTO picManage (picName,picSize,picWidth,picHeight,picType,picBinary,picThumb)";
   strSQL +=  " VALUES (@picName,@picSize,@picWidth,@picHeight,@picType,@picBinary,@picThumb)";
   
   
//   strSQL="INSERT INTO picManage (picName,picSize,picWidth,picHeight,picType,picBinary)";
//   strSQL +=  " VALUES ( '" + strFileName + "'," + fileLength + ",800,600,'" + fileType + "',"+ nPIC + ")";
  
   SqlCommand objCommand=new SqlCommand (strSQL,objConnection);

   SqlParameter paramName = new SqlParameter("@picName",SqlDbType.VarChar,50);
   paramName.Value = strFileName;
   objCommand.Parameters.Add( paramName );

   SqlParameter paramSize = new SqlParameter("@picSize",SqlDbType.BigInt,8);
   paramSize.Value = fileLength;
   objCommand.Parameters.Add( paramSize );

   SqlParameter paramWidth = new SqlParameter("@picWidth",SqlDbType.Int,4);
   paramWidth.Value = nWidth;
   objCommand.Parameters.Add( paramWidth );

   SqlParameter paramHeight = new SqlParameter("@picHeight",SqlDbType.Int,4);
   paramHeight.Value = nHeight;
   objCommand.Parameters.Add( paramHeight );

   SqlParameter paramType = new SqlParameter("@picType",SqlDbType.VarChar,50);
   paramType.Value = fileType;
   objCommand.Parameters.Add( paramType );

   SqlParameter paramBinary = new SqlParameter("@picBinary",SqlDbType.Image);
   paramBinary.Value = fileData;
   objCommand.Parameters.Add( paramBinary );

   SqlParameter paramThumb = new SqlParameter("@picThumb",SqlDbType.Image);
   paramThumb.Value = fileDataThumb;
   objCommand.Parameters.Add( paramThumb );


   //打开数据库,保存数据
   objConnection.Open();
   try
   {
    objCommand.ExecuteNonQuery();
   }
   catch(Exception ex)
   {
    
    Response.Write("<script defer>alert(/"数据保存失败!/")</script>");
    throw ex;
   }
   objConnection.Close();
   Response.Write("<script defer>alert(/"数据保存成功!/")</script>");
  

  }

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝天蜻蜓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值