请问如何在把图片保存到数据库时让图片变小就跟ps改变图像大小那样,尺寸而不改变图片本身现在一张图片一般都3mb左右

解决方案 »

  1.   

    C#将图片像素变低,就可以变小了
    http://topic.csdn.net/u/20100506/23/006f8270-4991-4279-ab20-0c274a76cb98.html?13579
      

  2.   

    像素变低就改变图片的质量了,可以先把图片读成byte[],然后可以使用 GZip 等压缩方法进行压缩,可以有效压缩;const int UNZIP_BUFFER_LENGTH = 1024 * 1024;        /// <summary>压缩数据流</summary>
            /// <param name="fileByteArray">需要压缩的数据流</param>
            /// <returns>返回压缩后的数据流</returns>
            public static byte[] ZipFileStream(byte[] fileBytes)
            {
                if (fileBytes == null) return null;
                // 压缩数据流
                MemoryStream memStream = new MemoryStream();
                GZipStream zipStream = null;            try
                {
                    // 创建压缩数据流
                    zipStream = new GZipStream(memStream, CompressionMode.Compress);
                    // 从指定的字节数组中将压缩的字节写入基础流
                    zipStream.Write(fileBytes, 0, fileBytes.Length);
                }
                finally
                {
                    if (zipStream != null) zipStream.Close();
                    // 关闭压缩数据流
                    memStream.Close();
                }
                return memStream.ToArray();
            }
      

  3.   

    等比例缩放图片
    <script language="JavaScript">
    var flag=false;
    function DrawImage(ImgD,iwidth,iheight){
      var image=new Image();
      image.src=ImgD.src;
      if(image.width>0 && image.height>0){
      flag=true;
      if(image.width/image.height>= iwidth/iheight){
      if(image.width>iwidth){   
      ImgD.width=iwidth;
      ImgD.height=(image.height*iwidth)/image.width;
      }else{
      ImgD.width=image.width;   
      ImgD.height=image.height;
      }
      ImgD.alt=image.width+"×"+image.height;
      }
      else{
      if(image.height>iheight){   
      ImgD.height=iheight;
      ImgD.width=(image.width*iheight)/image.height;   
      }else{
      ImgD.width=image.width;   
      ImgD.height=image.height;
      }
      ImgD.alt=image.width+"×"+image.height;
      }  }
    }onload="javascript:DrawImage1(this)">
    上传图片生成缩略图
    public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
    {
    System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
    int towidth = width;
    int toheight = height;
    int x = 0;
    int y = 0;
    int ow = originalImage.Width;
    int oh = originalImage.Height;
    switch (mode)
    {
    case "HW":   
    break;
    case "W":
    toheight = originalImage.Height * width / originalImage.Width;
    break;
    case "H":
    towidth = originalImage.Width * height / originalImage.Height;
    break;
    case "Cut":
    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
    {
    oh = originalImage.Height;
    ow = originalImage.Height * towidth / toheight;
    y = 0;
    x = (originalImage.Width - ow) / 2;
    }
    else
    {
    ow = originalImage.Width;
    oh = originalImage.Width * height / towidth;
    x = 0;
    y = (originalImage.Height - oh) / 2;
    }
    break;
    default:
    break;
    }
    System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
    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(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
    new System.Drawing.Rectangle(x, y, ow, oh),
    System.Drawing.GraphicsUnit.Pixel);try
    {
    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    catch (System.Exception e)
    {
    throw e;
    }
    finally
    {
    originalImage.Dispose();
    bitmap.Dispose();
    g.Dispose();
    }

    重画
    Image oldImg = new Bitmap("d:/a.jpg");
    Image newImg = new Bitmap(oldImg.Width, oldImg.Height - 100);Graphics g = Graphics.FromImage(newImg);
    g.DrawImage(oldImg, new Point(0, 0));