Solved

SMALL Jpeg unit, lib, or component

Posted on 2004-03-29
16
401 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
  • 7
  • 6
  • 3
16 Comments
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
Good Luck!

Shane
0
 
LVL 33

Expert Comment

by:Slick812
Comment Utility
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
Comment Utility
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 33

Expert Comment

by:Slick812
Comment Utility
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 33

Expert Comment

by:Slick812
Comment Utility
?? 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:thousandjulys
Comment Utility
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
Comment Utility
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 33

Accepted Solution

by:
Slick812 earned 200 total points
Comment Utility
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
Comment Utility
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 33

Expert Comment

by:Slick812
Comment Utility
??? It work fine for me
0
 
LVL 33

Expert Comment

by:Slick812
Comment Utility
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 33

Expert Comment

by:Slick812
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now