Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to print a jpg image? How to convert a jpg to bmp?

Status
Not open for further replies.

DoNotEven

Programmer
Dec 21, 2000
89
0
0
US
The following code returns this error when it trys to print a jpg image: Cannot assign a TPicture to a Bitmap.void __fastcall TForm1::printphotoClick(TObject *Sender)
{
unsigned int BitmapInfoSize, BitmapImageSize;
long DIBWidth, DIBHeight;
PChar BitmapImage;
Windows::pBitmapInfo BitmapInfo;
Graphics::TBitmap *Bitmap;
Bitmap = new Graphics::TBitmap();
TImage *pImage;
pImage = Image1;
Printer()->BeginDoc();
Bitmap->Assign(pImage->Picture);
GetDIBSizes(Bitmap->Handle, BitmapInfoSize, BitmapImageSize);
BitmapInfo = (PBitmapInfo) new char[BitmapInfoSize];
BitmapImage = (PChar) new char [BitmapImageSize];
GetDIB(Bitmap->Handle, 0, BitmapInfo, BitmapImage);
DIBWidth = BitmapInfo->bmiHeader.biWidth;
DIBHeight = BitmapInfo->bmiHeader.biHeight;
StretchDIBits(Printer()->Canvas->Handle,
0, 0, DIBWidth, DIBHeight,
0, 0, DIBWidth, DIBHeight,
BitmapImage, BitmapInfo,
DIB_RGB_COLORS, SRCCOPY);
Printer()->EndDoc();
delete [] BitmapImage;
delete [] BitmapInfo;
delete Bitmap;
}

So how do I print a jpg image that is in Image1?
pImage = Image1; prints if it is a bmp but not if it is a jpg.

And how do I convert a jpg to a bitmap?

Please help me out.

Thanks

 
Check out this link: . Also, they hve a couple more helps with JPEG's at their site. Do a search for JPEG or search through their FAQs and Articles.

James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Thanks James

But I still do not have my questions answered.
Please someone help me out.

Thanks,
Bob

 
Hi,

It's a good question and I'm facing the same problem as you. I'm afraid this is again a partial answer...

The problem is that the JPEG format is not a built-in standard in Windows. The conversion cannot be done by the system WIN32 itself.

Applications typically use some DLLs which provide the JPEG data handling features. Every single PC able to properly display a JPEG image certainly has at least one of such a DLL. It is fairly easy to locate one in your Windows/system directory.

The problem is that to make use of it you need to know the interface of the JPEG routines in it (i.e header file). What I'll do as soon as I have time is to investigate the assembly code of such a DLL, in order to find out the underlying routine format. Success is not guaranteed and it can take time.

On websites such as "programmer's heaven" ( you can find some free DLLs provided with the corresponding header files. (Once I found one named "jpegapi.dll"; however it pops up a nag screen...).

Let us know if you find out more ...

Woliwol
 
Woliwol

I will figure it out, regardless of how long it takes. If I could figure out how to convert a JPG to a BMP I could make that work for me.

I found code that converts a BMP to JPG "if it works" have not tried it yet:
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//Requires "jpeg.hpp" to be included in the source file
TJPEGImage *jp = new TJPEGImage();
try
{
jp->Assign(Image1->Picture->Bitmap);
jp->SaveToFile("c:\\oneeye.jpg");
}
__finally
{
delete jp;
}
}

Now to reverse the procedure?

void __fastcall TForm1::Button2Click(TObject *Sender)
{
Graphics::TBitmap *bm = new Graphics::TBitmap();
try
{
bm->Assign(Image1->Picture->Bitmap);
//what should replace Bitmap? Because now it is a JPEG
//that is loaded in Image1->Picture
bm->SaveToFile("c:\\oneeye.bmp");
}
__finally
{
delete bm;
}
}

I tried a simple conversion which does not work:

Image1 has a JPEG in it.
Image1->Picture->SaveToFile("C:\\temp.bmp");
Image1->Picture->LoadFromFile("C:\\temp.bmp");

All this did was to change the file extension, the file was still a JPEG.

HELP, please.

Bob
 
How to convert a JPG to a BMP.

I figured it out as follows which works perfect:

#include <jpeg.hpp> //has to be included in your .cpp file

void __fastcall TForm1::Button1Click(TObject *Sender)
{
Graphics::TBitmap *bmp = new Graphics::TBitmap();
TJPEGImage *jpg = new TJPEGImage();
try
{
jpg->LoadFromFile(&quot;C:\\yourfile.jpg&quot;);
jpg->DIBNeeded();
bmp->Assign(jpg);
Image1->Picture->Assign(bmp);
Image1->Picture->SaveToFile(&quot;C:\\yourfile.bmp&quot;);
}
__finally
{
delete bmp;
delete jpg;
}
}

Now I can do anything I want with yourfile.bmp....like print it!

It may be the wrong way but it works perfect!

Bob
 
If it works it can't be all that wrong! X-) James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
I need to print a 24-bit bitmap file inside of a program that I'm writing in MSVC++ 6.0? Any ideas?

 
I do not use MSVC. But in Builder 5 here is a help file that may help you.

Indicates the bit format of the bitmap image, specifying how the image is displayed and how the pixels of the bitmap image are stored in memory.

enum TPixelFormat {pfDevice, pf1bit, pf4bit, pf8bit, pf15bit, pf16bit, pf24bit, pf32bit, pfCustom};
__property TPixelFormat PixelFormat = {read=GetPixelFormat, write=SetPixelFormat, nodefault};

Description

Use PixelFormat to change a TBitmap's internal image to a particular memory format and color depth, or to find out what memory format and color depth a TBitmap is using.

For example, PixelFormat can be used to set the pixel format of the bitmap image to 8-bit for video drivers that cannot display the native format of a bitmap image.

Changing the pixel format is most commonly used with ScanLine, because your code must decode the pixel data accessed by ScanLine. Image-editing tools usually use one pixel for all internal image operations and copy the results to the screen (in whatever format) as the last step.

*******************

Provides indexed access to each line of pixels.

__property void * ScanLine[int Row] = {read=GetScanline};

Description

ScanLine is used only with DIBs (Device Independent Bitmaps) for image editing tools that do low-level pixel work.

Example:

void __fastcall TForm1::Button1Click(TObject *Sender)

{
Graphics::TBitmap *pBitmap = new Graphics::TBitmap();
// This example shows drawing directly to the Bitmap
Byte *ptr;
try
{
pBitmap->LoadFromFile(&quot;C:\\Program Files\\Common Files\\Borland Shared\\Images\\Splash\\256color\\factory.bmp &quot;);
for (int y = 0; y < pBitmap->Height; y++)
{
ptr = (Byte *)pBitmap->ScanLine[y];
for (int x = 0; x < pBitmap->Width; x++)

ptr[x] = (Byte)y;
}
Canvas->Draw(0,0,pBitmap);
}
catch (...)
{
ShowMessage(&quot;Could not load or alter bitmap&quot;);
}
delete pBitmap;
}

Hope that helps you.



 
Hallo, ich möchte ein JGP mit der Auflösung 1600x1200 Pixel in ein JPG mit der Auflösung 360x270 Pixel umwandeln.
Wer hat eine Idee.

Danke Ralf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top