Help me resize JPG image without getting crap result lazarus / 13-General ---------------------------------------------------------------------- beli0135 · 2006-08-30 13:18 Hi to everyone I am having serious problem resizing JPG. I am using following code: Code: [Select] bmp := TBitmap.create;   try     bmp.Width := AWidth;     bmp.Height := AHeight;     jpg := TJPEGImage.create;     try       jpg.LoadFromFile(infile);       bmp.canvas.StretchDraw(Rect(0,0,AWidth,AHeight),jpg);     finally       jpg.Free;     end;     jpg := TJPEGImage.create;     try        jpg.CreateFromBitmapHandles(bmp.Handle,0,Rect(0,0,AWidth,AHeight));        jpg.CompressionQuality := AQuality;        jpg.SaveToFile(OutFile);     finally       jpg.Free;     end;   finally     if assigned(bmp) then bmp.Free;   end; problem occures here: bmp.canvas.StretchDraw(Rect(0,0,AWidth,AHeight),jpg); if I save BMP immediately after that, it is fuzzy. Anyone knows how to do it? ---------------------------------------------------------------------- felipemdc · 2006-08-30 15:42 One possible work around in not to StretchDraw the JPG (didn´t test it). Just copy it exactly like it is (Draw function) to a temporary bitmap, and then stretchDraw that bitmap into another one. ---------------------------------------------------------------------- beli0135 · 2006-08-30 21:39 Hum... looks like a botch job, but since I do not have another idea, I will give it a try. Thanks ---------------------------------------------------------------------- alanphys · 2006-09-12 06:10 What operating system are you using? I have the same problem using FC4 and GTK1 and 2 which I posted a while back without getting much response. My application uses a bitmap that you can zoom in on. Under FC4 and GTK1 and 2 it is terribly blurred. The strange thing is I recompiled it under windows and it worked perfectly. The problem seems to be in Lazarus' implementation of StretchDraw on GTK as GTK itself doesn't have this problem. The primitives used by the StretchDraw operation interpolate between pixels. For a good resolution image this gives a nice smooth result but for a pixelated or noisy image the result is awful. As far as I can determine the operation of the primitives is controlled by the ROP (raster operation) parameter. This is as far as I have been able to get without going round in circles in the limited time available to me. Perhaps one of the gurus can give us a nudge in the right direction? Hope this helps Alanphys ---------------------------------------------------------------------- alanphys · 2006-10-10 12:50 Sorry, I was wrong. The scaling of the image is not controlled by ROP, but by the following lines in function ScalePixMap in GTKProc.inc:   // calculate ScaleMethod   ShrinkWidth := NewWidth < SrcWidth;   ShrinkHeight := NewHeight < SrcHeight;   //GDKPixbuf Scaling is not done in the same way as Windows   //but by rights ScaleMethod should really be chosen based   //on the destination device's internal flag   {GDK_INTERP_NEAREST,GDK_INTERP_TILES,   GDK_INTERP_BILINEAR,GDK_INTERP_HYPER);}   If ShrinkWidth and ShrinkHeight then     ScaleMethod := GDK_INTERP_TILES   else     If ShrinkWidth or ShrinkHeight then       ScaleMethod := GDK_INTERP_BILINEAR//GDK_INTERP_HYPER     else       ScaleMethod := GDK_INTERP_TILES;  //GDK_INTERP_BILINEAR; If you change the last line above from ScaleMethod := GDK_INTERP_BILINEAR to GDK_INTERP_TILES and rebuild Lazarus you should see a sharper image. This is not an ideal solution as each time you upgrade Lazarus you have to change it. Cheers Alanphys