Solved

SMALL Jpeg unit, lib, or component

Posted on 2004-03-29
16
410 Views
Last Modified: 2010-04-05
im looking for a SMALL jpeg unit, library or component that can take a bitmap handle (HBitmap) and save it as a jpeg file. needs to be SMALL and doesnt use any vcl units for a small project. thanks!
0
Comment
Question by:thousandjulys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
16 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10710003
Ok, first,

OTTOMH

Uses Graphics, Jpeg;


TempBitmap := TBitmap.Create;
TempBitmap.Handle := HBITMAP;
Canvas.Draw(0, 0, TempBitmap);
with TJPegImage.Create do
 begin
  Assign(tempBitMap);
  SaveToFile('Filename.jpg');
  Free;
 end;
TempBitmap.Free;

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10710466
Or something like this:

{...}
MemBitmap        := TBitmap.Create;
MemBitmap.Handle := "HBITMAP";

Image1.Width  := MemBitmap.Width;
Image1.Height := MemBitmap.Height;
Image1.Canvas.Draw(0, 0, MemBitmap);

with TJPegImage.Create do
 begin
  Assign(Image1.Picture.Bitmap);
  SaveToFile('Filename.jpg');
  Free;
 end;


MemBitmap.Free;
{...}



0
 

Author Comment

by:thousandjulys
ID: 10710479
i dont want a procedure with delphi's jpeg component... that uses VCLs, i want a component that uses no vcls to produce a small project.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:shaneholmes
ID: 10710511
Good Luck!

Shane
0
 
LVL 34

Expert Comment

by:Slick812
ID: 10710728
hello thousandjulys, The Jpeg compression and decompression code is very complex and long, if you look for the JPEG unit code in delphi it is not there, because most of it in in pre-made obj files. . . and these jpeg code alone will add about 70-90  Kilobytes to your executable, these JPEG will depend on a system DIB (bitmap) so you will need code to handle a system DIB, and that is not so easy, I have done a minor version. . . You may as well use the Graphics Unit and the Jpeg unit of Delphi (adding 120 Kilobytes or more) to your executable, because I looked and I could NOT find anything even close to what you are asking for, some dicussion about this is at this EE question - -

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20752685.html

If you are interested in API delphi programming you might look at my site -

http://www.angelfire.com/hi5/delphizeus/index.html
0
 

Author Comment

by:thousandjulys
ID: 10717264
i already have a dcu file thats about 70k that has a few obj files, but it messes up the image if its not in 16bit, how bout is there any way to convert a bitmap handle of a 32bit image to 16 bit?
0
 
LVL 34

Expert Comment

by:Slick812
ID: 10720914
I'm not to sure about what you are asking? ? You can NOT ever convert a bitmap from one color depth (Format, bit number) to another, what you will need to do is create another different bitmap of whatever color format you need, and then BitBlt the first bitmap on the second bitmap and then DeleteObject( ) on the first bitmap. Replace the first bitmap with a copy (in the required format). This is what happens in the delphi graphics unit when you change the pixel format of a TBitmap. I have some other code here at EE for a DIB creation, you can look at this question -

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20752840.html


I think there is enough there for you to create a DIB of any pixel bit format (16 bit), even if you do not need to save it to file, I may have some time tommorow to get you some code to do a 16 bit DIB creation and BitBlt the orginal hBitmap on it
0
 
LVL 34

Expert Comment

by:Slick812
ID: 10720924
?? for my own info, where did you find this  "dcu file thats about 70k" that does JPEG compression? ? Many of the JPEG stuff I have seen will take a 24 bit bitmap, not a 16 bit
0
 

Author Comment

by:thousandjulys
ID: 10726741
LOMJpeg is the small jpeg lib (http://www.lommage.co.uk/), and what you explained on how to move one bitmap with a certain color depth to another, is pretty much converting it.
0
 

Author Comment

by:thousandjulys
ID: 10726791
and yeah if you have a procedure that converts a handle to a bitmap thats 32bit and rewrites it on a 24bit (24 is fine too) that would be nice. would it be possible to do it without creating any files?
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 200 total points
ID: 10727830
OK, here's a create 16 DIB with as little code as I could get, the hBmp is just a Source bitmap that this will get the width and height from. . . But this DOES not change  the hBmp in any way. This function will Return a new 16 Bit bitmap handle. . .

function Make16BitBmp(hBmp: Integer): Integer;
var
cDC1: Cardinal;
DibSec: TDIBSection;
BmpInfo1: TBitmapInfo;
pBits: Pointer;
begin
Result := 0;
if hBmp < 32 then Exit;
ZeroMemory(@DibSec.dsBm, SizeOf(DibSec.dsBm));
if GetObject(hBmp, SizeOf(DibSec), @DibSec) = 0 then
  begin
  MessageBox(0, 'ERROR - GetObject Failed for hBmp, not a valid system Bitmap handle',
             'ERROR, GetObject', MB_ICONERROR);
  Exit;
  end;

ZeroMemory(@BmpInfo1, sizeof(BmpInfo1));

with BmpInfo1.bmiHeader do
  begin
  biSize := SizeOf(TBitmapInfoHeader);
  biWidth := DibSec.dsBm.bmWidth;
  biHeight := abs(DibSec.dsBm.bmHeight);
  biPlanes := 1;
  biBitCount := 16;
  biSizeImage := ((biWidth * biBitCount) + 31) and not 31;
  biSizeImage := Integer((biSizeImage shr 3)) * biHeight;
  end;

cDC1 := CreateCompatibleDC(0);
pBits := nil;
Result := CreateDIBSection(cDC1, BmpInfo1, DIB_RGB_COLORS, pBits, 0, 0);
DeleteDC(cDC1);

if Result = 0 then
  MessageBox(0, 'ERROR - CreateDIBSection Failed, Could not create a Bitmap',
             'ERROR, CreateDIBSection', MB_ICONERROR);
end;

 - - - - - - - - - - - - - - - - - - -

you will still need to BitBlt the hBmp onto this new Bitmap and then DeleteObject( ) for the hBmp
hNew16Bmp := Make16BitBmp(hOld32Bmp);
0
 

Author Comment

by:thousandjulys
ID: 10729390
im trying to do that BitBlt but it keeps ending up just leaving the newly made 16bit bitmap all black. any tips on that procedure :-)
0
 
LVL 34

Expert Comment

by:Slick812
ID: 10729533
??? It work fine for me
0
 
LVL 34

Expert Comment

by:Slick812
ID: 10729571
It seems that you are not familar with API bitmap coding, you will need to be somewhat able to do API Bitmap, if you want to do this kind of thing, it ain't delphi bitmap

I am assuming that the   h32Bmp    is a valid system bitmap handle, although it does NOT matter what color depth it is , it can be 32, 24 bit or other


hNewBmp := Make16BitBmp(aBmp.Handle);

aDC1 := CreateCompatibleDC(0);
SelectObject(aDC1, hNewBmp);
bDC2 := CreateCompatibleDC(0);
SelectObject(bDC2,  h32Bmp);
BitBlt(aDC1, 1, 1, aBmp.width, aBmp.Height, bDC2, 0, 0, SRCCOPY);
DeleteDC(aDC1);
DeleteDC(bDC2);

 - - - - - - - - - - - - - - - - - - - - -

I read the Read me file on the LOMJpeg thing, and I do NOT think that is a good Idea, you should only work with 24 bit bitmaps with a JPEG conversion, that is the native format for the JPeg file bitmap to use (At least for versions of JPEG that are not "NEW" and since this craps out on 32 bit, then it is NOT NEW)

so you might be better off if you change the line

biBitCount := 16;

to

biBitCount := 24;
0
 
LVL 34

Expert Comment

by:Slick812
ID: 10729594
OK, I copied and pasted the BitBlt thing
you will need to add the current width and height

BitBlt(aDC1, 0, 0, WhateverWidth, whateverHeight, bDC2, 0, 0, SRCCOPY);

if you draw a mental Zero on the width and height then you might try this - -

ZeroMemory(@DibSec.dsBm, SizeOf(DibSec.dsBm));
GetObject( h32Bmp, SizeOf(DibSec), @DibSec) ;
aDC1 := CreateCompatibleDC(0);
SelectObject(aDC1, hNewBmp);
bDC2 := CreateCompatibleDC(0);
SelectObject(bDC2,  h32Bmp);
BitBlt(aDC1, 0, 0, DibSec.dsBm.biWidth, DibSec.dsBm.biHeight, bDC2, 0, 0, SRCCOPY);
DeleteDC(aDC1);
DeleteDC(bDC2);
0
 

Author Comment

by:thousandjulys
ID: 10734928
i know its not delphi bitmap buddy, u dont have to tell me what i think, especially when ur wrong. all i wanted was a sample, because all the sites ive googled are nonsense. thanks for the help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question