An error occurred while loading the image. Please take a look! Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiMultimedia/html/delphi_20061103170415180.html
Fbmp: tbitmap;
Fbmp. loadfromfile ('C: \ abc.bmp ');
When this step is executed, an error is reported, indicating that an error occurred while reading stream. Image abc.bmp is compressed from 24 bits to 8 bits by Photoshop.
If the image is not compressed, no error will be reported, but the compressed image is still a BMP image, which can be opened in windows, that is
I cannot open it in Delphi and cannot load it directly in the timage of the control in Delphi. Which one knows the reason!
VaR
Fbmp: tbitmap;
Begin
Fbmp: = tbitmap. Create;
Fbmp. loadfromfile ('C: \ abc.bmp ');
Fbmp: = tbitmap. Create;
MyCodeFbmp: = tbitmap. Create; is executed, but I didn't write it here. The problem is not about creating image objects. Please kindly advise!
Check the error code.
Check the format of pixelformat settings.
It seems that the image cannot be processed too much. I tried to process more than 4 MB of images. It should be because the image is too large.
The image size has not exceeded 100 kb; it should not be a problem!
The format can be set only after the image is loaded, because an error is reported during loading, so it is not pixelformat!
I wonder which one can help me solve this problem ......
The built-in tbitmap of Delphi does not support this compression format.
Other image libraries must be used
An error is reported when loading the image control timage directly in Delphi!
Include the following units in your Program .
Unit gdiplusbitmap;
Interface
Uses
Windows, sysutils, ActiveX, classes, graphics;
Type
Tgdipbitmap = Class (tbitmap)
Public
Procedure loadfromstream (Stream: tstream); override;
Procedure savetofile (const filename: string); override;
Procedure savetostream (Stream: tstream); override;
End;
Implementation
Const
Gpdll = 'gdiplus. dll ';
Type
Tstatus = integer;
Pgdiplusstartupinput = ^ tgdiplusstartupinput;
Tgdiplusstartupinput = packed record
Gdiplusversion: integer;
Debugeventcallback: pointer;
Suppressbackgroundthread: bool;
Suppressexternalcodecs: bool;
End;
Timagecodecinfo = packed record
CLSID: tguid; // identifies the guid of a specific decoder.
Formatid: tguid; // identifies the guid In the decoder format.
Codecname: pwchar; // The name string of the codec.
Dllname: pwchar; // The path name string of the DLL that stores the decoder
Formatdescription: pwchar; // a string that describes the file format of the decoder.
Filenameextension: pwchar; // string of the file extension used in the decoder
Mimetype: pwchar; // multi-purpose Internet Mail Extension protocol (MIME) string of the decoder
Flags: DWORD; // other information about the decoder (a combination of several logos of imagecodecflags)
Version: DWORD; // the version number of the decoder.
Sigcount: DWORD;
Sigsize: DWORD;
Sigpattern: pbyte; // represents a two-dimensional array of bytes signed by the decoder.
Sigmask: pbyte; // a two-dimensional byte array used as a filter
End;
Pimagecodecinfo = ^ timagecodecinfo;
Function gdiplusstartup (VAR token: DWORD; const input: pgdiplusstartupinput; output: pointer): tstatus;
Stdcall; External gpdll name 'gdiplusstartup ';
Procedure gdiplusshutdown (token: DWORD );
Stdcall; External gpdll name 'gdiplusshutdown ';
Function gdipcreatebitmapfromstream (Stream: istream; var bitmap: pointer): tstatus;
Stdcall; External gpdll name 'gdipcreatebitmapfromstream ';
Function gdipcreatebitmapfromhbitmap (HBM: hbitmap; hpal: hpalette; var bitmap: pointer): tstatus;
Stdcall; External gpdll name 'gdipcreatebitmapfromhbitmap ';
Function gdipsaveimagetostream (image: pointer; stream: istream; const clsidencoder: pguid; const encoderparams: pointer): tstatus;
Stdcall; External gpdll name 'gdipsaveimagetostream ';
Function gdipdisposeimage (image: pointer): tstatus;
Stdcall; External gpdll name 'gdipdisposeimage ';
Function gdipcreatehbitmapfrombitmap (Bitmap: pointer; var hbmreturn: hbitmap; Background: DWORD): tstatus;
Stdcall; External gpdll name 'gdipcreatehbitmapfrombitmap ';
Function gdipgetimageencoderssize (VAR numencoders, size: integer): tstatus;
Stdcall; External gpdll name 'gdipgetimageencoderssize ';
Function gdipgetimageencoders (numencoders, size: integer; encoders: pimagecodecinfo): tstatus;
Stdcall; External gpdll name 'gdipgetimageencoders ';
Function gdipgetimagepixelformat (image: pointer; var format: integer): tstatus;
Stdcall; External gpdll name 'gdipgetimagepixelformat ';
Type
Tgpbitmap = Class
Public
Native: pointer;
Constructor create (Stream: istream); overload;
Destructor destroy; override;
Constructor create (HBM: hbitmap; hpal: hpalette); overload;
Function gethbitmap: hbitmap;
Function getpixelformat: integer;
Procedure save (Stream: istream; const clsidencoder: tclsid );
End;
Procedure checkstatus (Status: tstatus );
Begin
If status <> 0 then
Raise exception. Create ('gdiplus error! ');
End;
{Tgpbitmap}
Constructor tgpbitmap. Create (Stream: istream );
Begin
Checkstatus (gdipcreatebitmapfromstream (stream, native ));
End;
Constructor tgpbitmap. Create (HBM: hbitmap; hpal: hpalette );
Begin
Checkstatus (gdipcreatebitmapfromhbitmap (HBM, hpal, native ));
End;
Destructor tgpbitmap. Destroy;
Begin
Gdipdisposeimage (native );
End;
Function tgpbitmap. gethbitmap: hbitmap;
Begin
Checkstatus (gdipcreatehbitmapfrombitmap (native, result, 0 ));
End;
Function tgpbitmap. getpixelformat: integer;
Begin
Checkstatus (gdipgetimagepixelformat (native, result ));
End;
Procedure tgpbitmap. Save (Stream: istream; const clsidencoder: tclsid );
Begin
Checkstatus (gdipsaveimagetostream (native, stream, @ clsidencoder, nil ));
End;
VaR
Gdiplusstartupinput: tgdiplusstartupinput;
Gdiptoken: DWORD;
Imageformat: String = '';
Procedure setimageformat (const filename: string );
Begin
Imageformat: = extractfileext (filename );
If imageformat <> ''then
Begin
Delete (imageformat, 1, 1 );
If comparetext (imageformat, 'jpg ') = 0 then
Imageformat: = 'jpeg'
Else if comparetext (imageformat, 'tif ') = 0 then
Imageformat: = 'tiff ';
End else imageformat: = 'bmp ';
Imageformat: = 'image/'+ imageformat;
End;
Function getimageencoderclsid (Format: string; var CLSID: tguid): integer;
VaR
Num, size, I: integer;
Imagecodecinfo: pimagecodecinfo;
Type
Infoarray = array of timagecodecinfo;
Begin
Num: = 0;
Size: = 0;
Result: =-1;
Gdipgetimageencoderssize (Num, size );
If (size = 0) Then exit;
Getmem (imagecodecinfo, size );
If (imagecodecinfo = nil) Then exit;
Try
Gdipgetimageencoders (Num, size, imagecodecinfo );
I: = 0;
While (I <num) and (comparetext (infoarray (imagecodecinfo) [I]. mimetype, format) <> 0) Do
INC (I );
If I <num then
Begin
CLSID: = infoarray (imagecodecinfo) [I]. CLSID;
Result: = I;
End;
Finally
Freemem (imagecodecinfo, size );
End;
End;
{Tgdipbitmap}
Procedure tgdipbitmap. loadfromstream (Stream: tstream );
VaR
Adaper: tstreamadapter;
TMP: tgpbitmap;
Begin
Adaper: = tstreamadapter. Create (stream, soreference );
TMP: = tgpbitmap. Create (adaper );
Try
Handle: = TMP. gethbitmap;
Case (TMP. getpixelformat SHR 8) and $ ff
1: pixelformat: = pf1bit;
4: pixelformat: = pf4bit;
8: pixelformat: = pf8bit;
16: pixelformat: = pf16bit;
24: pixelformat: = pf24bit;
32: pixelformat: = pf32bit;
Else pixelformat: = pfcustom;
End;
Finally
TMP. Free;
End;
End;
Procedure tgdipbitmap. savetofile (const filename: string );
Begin
Setimageformat (filename );
Inherited savetofile (filename );
Imageformat: = '';
End;
Procedure tgdipbitmap. savetostream (Stream: tstream );
VaR
TMP: tgpbitmap;
Adaper: tstreamadapter;
CLSID: tguid;
Begin
If (imageformat <> '') and (getimageencoderclsid (imageformat, CLSID) <>-1) then
Begin
TMP: = tgpbitmap. Create (handle, palette );
Try
Adaper: = tstreamadapter. Create (stream, soreference );
TMP. Save (adaper, CLSID );
Finally
TMP. Free;
End;
End else
Inherited savetostream (Stream );
End;
Initialization
Begin
Fillchar (gdiplusstartupinput, sizeof (gdiplusstartupinput), 0 );
Gdiplusstartupinput. gdiplusversion: = 1;
Gdiplusstartup (gdiptoken, @ gdiplusstartupinput, nil );
Tpicture. registerfileformat ('bmp ', 'bmp file', tgdipbitmap );
Tpicture. registerfileformat ('exif', 'tiff file', tgdipbitmap );
Tpicture. registerfileformat ('tiff ', 'tiff file', tgdipbitmap );
Tpicture. registerfileformat ('tif ', 'tiff file', tgdipbitmap );
Tpicture. registerfileformat ('png ', 'png file', tgdipbitmap );
Tpicture. registerfileformat ('gif', 'gif file', tgdipbitmap );
Tpicture. registerfileformat ('jpeg ', 'jpeg file', tgdipbitmap );
Tpicture. registerfileformat ('jpg ', 'jpg file', tgdipbitmap );
End;
Finalization
Begin
Tpicture. unregistergraphicclass (tgdipbitmap );
Gdiplusshutdown (gdiptoken );
End;
End.
Contains the above unit, compiled once, and can be directly loaded from the timage. If you use tbitmap, you can use the following code:
VaR
Fbmp: tbitmap;
TMP: tgdipbitmap;
Begin
TMP: = tgdipbitmap. Create;
TMP. loadfromfile ('C: \ abc.bmp ');
Fbmp. Assign (TMP );
TMP. Free;
End;
Pass
VaR
Fbmp: tbitmap;
TMP: tgdipbitmap;
Begin
TMP: = tgdipbitmap. Create;
TMP. loadfromfile ('C: \ abc.bmp ');
Fbmp: = tbitmap. Create;
Fbmp. Assign (TMP );
TMP. Free;
End
To: maozefa (AFA)
The program cannot run after the above Code is added!
The program cannot run after the above Code is added!
========================================================== ============
What operating system do you use?
I tried again. The above code is definitely correct under XP and 2000. Whether your image can be installed is another thing, but it should not be able to run. Unless your operating system is 98.
If your operating system is 98, you can download gdiplus. DLL to your system or your program directory.
My computer system is in Win2000, and I also searched gdiplus before running it. DLL. You can find gdiplus in the system. DLL, after adding gdiplusbitmap, can be compiled, but cannot run.
There is a GDI + package. You can download it and add the PAS file to your project. You can find it online.
To: hongqi162 (missing Moon)
Can you give me a specific name!
No one answered.
The built-in image does not support this compression format.
In Delphi, this is the case. Sometimes BMP can be opened in window, but it cannot be loaded in Delphi, or it may be that your current is not BMP, but the suffix is BMP, this file can be opened in the window, but Delphi will check whether the BMP file is true. If not, an error will be reported .....
^
Download the GDI + package:
Http://www.2ccc.com/article.asp? ArticleID = 3131
GDI + already supports very different image formats, Enhancing image processing and making it easier to use...