各位朋友,小弟的数据库是SQLSERVER2000的,其中有一个字段设置为Image字段,用来存放图像的,程序的主窗体上有一个Image控件,有来放图片和显示图片的,Image控件的大小为:高:235,宽:230,现在自己保存图片的代码如下,虽然也可以正常保存图片,但自己做过测试,保存几张图片之后,数据库的容量会增加几百K甚至过M,正常来讲,高为:235,宽为:230的JPEG图片,容量最多十来KB的,现在想请教一下论坛上的各位朋友,如何修改自己的代码,才可以使Image上面的图片,按照实际高度和宽度,高为:235,宽为:230,并且格式为JPEG写入数据库,请各位朋友赐教,谢谢!!!procedure TFrm_SO_New.Add_Picture(Aimage: TImage; i: integer); //将图片添加到IMAGE
var
  bmp: TBitmap;
  pic: TPicture;
  Jpg: TJpegImage;
  x, y: integer;
  Mem1,Mem2TMemoryStream;
begin
  bmp := TBitmap.Create;
  pic := TPicture.Create;
  Jpg := TJpegImage.Create;
  try
    if (ClipBoard.HasFormat(CF_PICTURE)) or
      (ClipBoard.HasFormat(CF_METAFILEPICT)) then
      begin {--判断格式}
        pic.Assign(Clipboard);
        X := pic.Width;
        Y := pic.Height;
        bmp.Width := X;
        bmp.Height := Y;
        bmp.Canvas.Draw(0, 0, pic.Graphic);
        Aimage.Picture.Bitmap := Bmp;
        Jpg.Assign(bmp);
      end;
    if (ClipBoard.HasFormat(CF_BITMAP)) then
      begin
        bmp.Assign(ClipBoard);
        Aimage.Picture.Bitmap := Bmp;
        Jpg.Assign(bmp);
     end;
    if i = 1 then //如果I等于1,就将这个图像存入流1  Mem1
      begin
        Mem1 := TMemoryStream.Create;
        Mem1.Position := 0;
        Jpg.SaveToStream(Mem1); //会在数据保存之后,对数据流进行释放
            with dm.Qry_Get do
             begin
                 close;
                 sql.Clear;
                 sql.Text := SQL语句,用来保存图像
                   Parameters[0].LoadFromStream(Mem1, ftblob);
                ExecSQL;
             end;
      end;
    if i = 2 then //如果I等于1,就将这个图像存入流2  Mem1
      begin
        Mem2 := TMemoryStream.Create;
        Mem2.Position := 0;
        Jpg.SaveToStream(Mem2); //会在数据保存之后,对数据流进行释放
            with dm.Qry_Get do
             begin
                 close;
                 sql.Clear;
                 sql.Text := SQL语句,用来保存图像
                   Parameters[0].LoadFromStream(Mem2, ftblob);
                ExecSQL;
             end;      end;  finally
    pic.Free;
    bmp.Free;
  end;
end;

解决方案 »

  1.   

    function TForm1.ConvertPICintoJPG(cPic: TPicture; jQuality, pWidth,
      pHeight: Integer): TJpegImage;
     var  tBMP:TBitmap;
    begin
      Result := TJpegImage.Create;
      if (pWidth >= 0) or (pHeight >= 0) then
      begin
        try
          tBMP := TBitmap.Create;  //创建一个过渡性BMP图片,用于更改图片尺寸
          if pWidth <= 0  then
            pWidth := cPic.Width;     //若pWidth为有效值则改变tBMP宽度,否则不变
          if pHeight <= 0 then
            pHeight := cPic.Height;   //若pHeight为有效值则改变tBMP高度,否则不变
          tBMP.Width := pWidth;
          tBMP.Height := pHeight;
          tBMP.Canvas.StretchDraw(tBMP.Canvas.ClipRect,cPic.Graphic); //按照新尺寸重画图形       Result.Assign(tBMP);
        finally
          tBMP.Free;
        end;
      end
      else
        Result.Assign(cPic);   //设置压缩质量
      Result.Compress;
      Result.CompressionQuality := jQuality;
    end;这是一个图片格式转换的例子,看下能不能行
      

  2.   

    终于完全解决了,谢谢各位朋友,代码如下:主要用了这两句:
            jpg.CompressionQuality := 50; //压缩图片
            jpg.Compress; //执行压缩procedure TFrm_SO_New.Add_Picture(Aimage: TImage; i: integer); //将图片添加到IMAGE
    var
      bmp: TBitmap;
      pic: TPicture;
      Jpg: TJpegImage;
      x, y, tmp_p: integer;begin
      tmp_p := 0; //这个是用来告诉程序是否继续做下一步的
      bmp := TBitmap.Create;
      pic := TPicture.Create;
      Jpg := TJpegImage.Create;
      try
        if (ClipBoard.HasFormat(CF_BITMAP)) then
          begin
            bmp.Assign(ClipBoard);
            Aimage.Picture.Bitmap := Bmp;
            jpg.CompressionQuality := 50; //压缩图片
            jpg.Compress; //执行压缩
            Jpg.Assign(bmp);
            tmp_p := 1; //接下来不用再判断了
       //     jpg.SaveToFile('c:\1.jpg');
          end;    if tmp_p = 0 then //这个变量还等于0,就是说,前面的还没有做过转换,还要继续做转换才行
          begin
            if (ClipBoard.HasFormat(CF_PICTURE)) or (ClipBoard.HasFormat(CF_METAFILEPICT)) then
              begin {--判断格式}
                pic.Assign(Clipboard);
                X := pic.Width;
                Y := pic.Height;
                bmp.Width := X;
                bmp.Height := Y;
                bmp.Canvas.Draw(0, 0, pic.Graphic);
                Aimage.Picture.Bitmap := Bmp;
                jpg.CompressionQuality := 50; //压缩图片
                jpg.Compress; //执行压缩
                Jpg.Assign(bmp);
             //   jpg.SaveToFile('c:\2.jpg'); {将图保存到C盘}
              end;
          end;    if i = 1 then //如果I等于1,就将这个图像存入流1  Mem1
          begin
            Mem1 := TMemoryStream.Create;
            Mem1.Position := 0;
            Jpg.SaveToStream(Mem1); //会在数据保存之后,对数据流进行释放
          end;
        if i = 2 then //如果I等于1,就将这个图像存入流2  Mem1
          begin
            Mem2 := TMemoryStream.Create;
            Mem2.Position := 0;
            Jpg.SaveToStream(Mem2); //会在数据保存之后,对数据流进行释放
          end;
      finally
        pic.Free;
        bmp.Free;
      end;end;