如何保存JPG图像为固定大小到 sql server2000 的 image资段?(200分)

  • 主题发起人 主题发起人 jlcsx
  • 开始时间 开始时间
J

jlcsx

Unregistered / Unconfirmed
GUEST, unregistred user!
如何保存JPG图像为固定大小到 sql server2000 的 image资段?<br><br>例如 JPG的文件为2M, 但我保存到数据库是能以30K大小的保存
 
那还有什么说的。你只有分割你的图像。先保存成文件再分割,然后保存到数据库。
 
供參考:<br>var<br> &nbsp;JPEGImage: TJPEGImage;<br> &nbsp;Picture: TPicture;<br> &nbsp;MemoryStream: TMemoryStream;<br>begin<br> &nbsp;if OpenPictureDialog1.Execute then<br> &nbsp;begin<br> &nbsp; &nbsp;if ADOQPrdtPic.State in [dsBrowse] then ADOQPrdtPic.Edit;<br> &nbsp; &nbsp;if OpenPictureDialog1.FilterIndex = 2 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;JPEGImage := TJPEGImage.Create;<br> &nbsp; &nbsp; &nbsp;Picture := TPicture.Create;<br> &nbsp; &nbsp; &nbsp;MemoryStream := TMemoryStream.Create;<br> &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp;Picture.LoadFromFile(OpenPictureDialog1.FileName);<br> &nbsp; &nbsp; &nbsp; &nbsp;JPEGImage.Assign(Picture.Bitmap);<br> &nbsp; &nbsp; &nbsp; &nbsp;JPEGImage.SaveToStream(MemoryStream);<br> &nbsp; &nbsp; &nbsp; &nbsp;(ADOQPrdtPic.FieldByName('PIC') as TBlobField).LoadFromStrea(MemoryStream);<br> &nbsp; &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp; &nbsp;JPEGImage.Free;<br> &nbsp; &nbsp; &nbsp; &nbsp;Picture.Free;<br> &nbsp; &nbsp; &nbsp; &nbsp;MemoryStream.Free;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;(ADOQPrdtPic.FieldByName('PIC') as TBlobField).LoadFromFil(OpenPictureDialog1.FileName);<br> &nbsp; &nbsp;ADOQPrdtPic.Post;<br> &nbsp;end;
 
没发现SQL Server 只能保存30K呀,我一直就是1个图像一个字段存储呀<br>还有,建议你不要存储在SQL Server 中,数据库很快就会爆炸的
 
保存到数据库的方式我知道,但如果每张图片都比较大,打开数据库会比较慢,<br>我的意思是先把图片的文件变小,比如降低分辨率的方式,然后在保存到数据库中。<br>因为我的图片是显示时做为1寸照片显示,分辨率低点没关系。
 
不如不存。只能保存几根头发
 
笨办法:<br>1将jpg格式转换成bmp<br> &nbsp; &nbsp; &nbsp; &nbsp;bmp := TBitmap.Create;<br> &nbsp; &nbsp; &nbsp; &nbsp;jpg := TJpegImage.Create;<br> &nbsp; &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jpg.LoadFromFile('f.jpg');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bmp.Assign(jpg);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bmp.SaveToFile('f.bmp');<br> &nbsp; &nbsp; &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bmp.Free;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jpg.Free;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br>2压缩bmp<br> &nbsp;b1 := TBitmap.Create;<br> &nbsp;b2 := TBitmap.Create;<br> &nbsp;try<br> &nbsp; &nbsp; &nbsp;b1.LoadFromFile('f.bmp');<br> &nbsp; &nbsp; &nbsp;b2.PixelFormat := b1.PixelFormat;<br> &nbsp; &nbsp; &nbsp;b2.width := round(b1.width*sqrt(所需大小/实际大小));<br> &nbsp; &nbsp; &nbsp;b2.height := round(b1.height*sqrt(所需大小/实际大小));<br> &nbsp; &nbsp; &nbsp;b2.Canvas.StretchDraw(Rect(0,0,b2.width,b2.height),b1);<br> &nbsp; &nbsp; &nbsp;b2.SaveToFile('f.bmp');<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;b1.free;<br> &nbsp; &nbsp;b2.free;<br> &nbsp;end;<br><br>3将bmp转换成jpg<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bmp := TBitmap.Create;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jpg := TJpegImage.Create;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bmp.LoadFromFile('f.bmp');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jpg.Assign(bmp);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jpg.JPEGNeeded;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jpg.SaveToFile('f.jpg');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bmp.Free;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jpg.Free;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>至于控制jpg到固定大小由于不同图象压缩比例不同,不太清楚
 
换个方式想这个问题吧,starxes的苯招也不错。。。[:D]
 
后退
顶部