Transparent bitmaps as resources? - delphi

I want to compile a resource file with brcc32 that has a 32bit Bitmap and I receive an error message: Error: Invalid bitmap format. If I save the bitmap in 24bit format, it is copiled successfully, but I lose transparency... It is really not possible to have transparent bitmap as resources or I miss something ?

brcc32 does not support creating 32bit BITMAP resources. So either
use a different resource compiler/editor that supports 32bit bitmaps.
create the bitmap resource as an RCDATA resource instead of a BITMAP resource.

Related

How to create animated GIF files, from bitmaps, that are compatible with all media-players?

I am trying to create an animated GIF file from 8 bit-pixel bitmaps, and save it in a format compatible with Windows Media Player and VideoLan VLC, using Anders Melander's GIFImage.Pas library and Graphics system library.
The result, for now, is a file that contains no errors, but is not animated. How can I save a series of 8-bit-pixel TBitMap images to an animated GIF file?
Here is the test library:
Unit Prova;
(* This GIF saving test make a GIF file that can't be opened with
Windows Media Player nor VideoLan VLC,
because it seems to has some errors *)
Interface
Uses Graphics,GIFImage; (* Copyright: (c) 1997-99 Anders Melander *)
Procedure SaveBitmaps(Picture1,Picture2,Picture3:TBitMap
(* Same graphics resolution, 8 Bit Pixel + Palette *);
GIFFN:String);
Implementation
Procedure SaveBitmaps(Picture1,Picture2,Picture3:TBitMap
(* Same graphics resolution, 8 Bit Pixel + Palette *);
GIFFN:String);
Var GIFImg:TGIFImage;
Begin
GIFImg:=TGIFImage.Create;
GIFImg.AnimationSpeed:=30;
GIFImg.Transparent:=False;
GIFImg.Animate:=True;
GIFImg.Width:=Picture1.Width;
GIFImg.Height:=Picture1.Height;
Try
GIFImg.Add(Picture1);
GIFImg.Add(Picture2);
GIFImg.Add(Picture3);
GifImg.OptimizeColorMap;
GifImg.Optimize([ooMerge,ooCrop],rmNone,dmNearest,0);
GIFImg.SaveToFile(GifFN);
Finally
GIFImg.Destroy;
End;
End;
End.
I've created a .GIF animated file named "Treno.Gif" that contains 320 frames at 8 bit-pixel, 320 pixel width and 200 pixel width, with this method, and i've tested it on the web at:
gif debugger world's simplest gif tool
The error that occours is:
Error: TypeError: Cannot read properties of null (reading 'delay')
The file is:
Treno.Gif
It was created with a tool, named Anim32, which I am still developing;
you can find it at:
Anim32 V.093
and help me solve any problems with the GIF format.
WARNING: I Want to use GIFImage.pas unit, made by Anders Melander, Filip Larsen and Reinier Sterkenburg and
not GIFImg that it is included in Delphi 2007 package!
The solution of Andreas Rejbrand:
Delphi TGIFImage animation issue with some GIF viewers
it is certainly valid, but, as I wrote, I need to use gifimage.pas and not gifimg.

How cand I include a bitmap in my custom component (if is possible)?

I am writing a component (a button) which needs a bitmap to be displayed on it. I don't want to make an ImageList property and the user assigns an image. I want that button to have only the image chosen by me.
I tried to include the bitmap in a resource file but when I try to access it I get "Resource not found" error message. This is what I've done:
myres.rc
FIXED BMP "fixed.bmp"
I compiled the resource file with: brcc32 myres.rc
Then I included it in my component unit...
implementation
{$R .\resources\myres.res}
And access it with...
MyComponent.Glyph.LoadFromResourceName(HInstance,'FIXED');
// MyComponent = class(TSpeedButton)
Edit1:
I deleted the {$R .\resources\myres.res} directive and I loaded the resource from menu Project -> Resources and it's working, both with HInstance or FindClassHInstance(MyComponent).
Using a resource editor I found that when I load the resource from the menu the resource appears with the name "FIXED" as it should, but when I load the resource compiled with brcc32 it appears with the name "0". It seems that brcc32 doesn't set the name correctly.
But I don't want to load it from menu, I want it to be loaded automatically with the component.
Edit2:
Remy Lebeau is correct. I was using a wrong BMP format (the file starts with 'BM6' characters instead 'BM8' like Photoshop produce it, and it works).
Change BMP to BITMAP in your RC file, and change HInstance to FindClassHInstance() in your code:
FIXED BITMAP "fixed.bmp"
Glyph.LoadFromResourceName(FindClassHInstance(MyComponent), 'FIXED');

TBitmap->LoadFromStream failed with Win XP

I'm using C++ Builder XE3 to develop a graph editor. All of the editing and drawing capabilities are made in a DLL that is loaded by the end user applications.
To store information about the available graph objects I use a SQLite database. That database contains BMP icons that are loaded into a TImageList at run-time.
Everything works fine with Win-7, Win-8 and Win-vista but with Win-XP a "Floating point division by 0" occurs when loading the bitmap. I use a temporary memory stream to load the blob from the database and then load it into a temporary TBitmap which is used to add the new icon into the final TImageList.
Here is the function used to do so...
void TIcons::AddMaskedBitmap( TImageList *ptImgList, unsigned char *pucIcon, unsigned int uiSize )
{
TMemoryStream *ptMemStream;
// Use a memory stream
ptMemStream = new TMemoryStream();
ptMemStream->Write( pucIcon, uiSize );
ptMemStream->Position = 0;//Seek( ( int )0, ( unsigned short )soBeginning );
// Load using the cached bmp object
m_ptBmp->Transparent = true;
#warning "floatting point division by 0 error with WinXP"
m_ptBmp->LoadFromStream( ptMemStream ); // floatting point division by 0 error with WinXP
// m_ptBmp->LoadFromFile( ".\\d.bmp" ); // works
// Create a mask
m_ptBmpMask->Assign( m_ptBmp );
m_ptBmpMask->Canvas->Brush->Color = m_ptBmp->TransparentColor;
m_ptBmpMask->Monochrome = true;
// Add it to the list
ptImgList->Add( m_ptBmp, m_ptBmpMask );
// Free mem
m_ptBmp->FreeImage();
m_ptBmpMask->FreeImage();
delete ptMemStream;
}
I've traced the TBitmap::LoadFromStream function and the exception occurs in the CreateDIBSection function.
To make sure the loaded bitmap files are saved using the right encoding I've tried to load them using the TBitmap::LoadFromFile function and it works fine, so I think there's something wrong with the TBitmap::LoadFromStream function but I can't figure out what !
If anyone has an idea...
Thanks.
LoadFromFile is implemented by creating a file stream, and passing that to LoadFromStream. This, if the contents of your memory stream are the same as the contents of the file, then the call to LoadFromStream will succeed.
Thus the only sane conclusion is that the contents of the memory stream are invalid in some way.
The bitmap stored into the database is encoded using the BITMAPV4HEADER structure which is supposed to be supported since Win95/NT4 but there's something wrong.
It works fine if I encode the bitmap using the BITMAPINFOHEADER structure which is an older version of bitmap encoding which does not contain color space information.
Just found out a solution that works for me.
My problem was that software that was developed on Win7, when running on XP was throwing the division by 0 error when loading one of my BMPs.
It turns out that the problematic BMP was saved using Win7 Paint (other BMPs that were ok were saved from Gimp).
All I needed to do to fix it was to open this BMP on XP Paint and save it from there.

How to add gif and tiff support to TJvDBImage?

TJvDBImage is a good component that support several picture formats. In JvJVCLUtils, it mentioned that the supported format can be expanded by RegisterGraphicSignature procedure. In the comment it mentioned :
WHAT IT IS:
These are helper functions to register graphic formats than can
later be recognized from a stream, thus allowing to rely on the actual
content of a file rather than from its filename extension.
This is used in TJvDBImage and TJvImage.
IMAGE FORMATS:
The implementation is simple: Just register image signatures with
RegisterGraphicSignature procedure and the methods takes care
of the correct instantiation of the TGraphic object. The signatures
register at unit's initialization are: BMP, WMF, EMF, ICO, JPG.
If you got some other image library (such as GIF, PCX, TIFF, ANI or PNG),
just register the signature:
RegisterGraphicSignature(<string value>, <offset>, <class>)
or
RegisterGraphicSignature([<byte values>], <offset>, <class>)
This means:
When <string value> (or byte values) found at <offset> the graphic
class to use is <class>
For example (actual code of the initialization section):
RegisterGraphicSignature([$D7, $CD], 0, TMetaFile); // WMF
RegisterGraphicSignature([1, 0], 0, TMetaFile); // EMF
RegisterGraphicSignature('JFIF', 6, TJPEGImage);
You can also unregister signature. IF you want use TGIFImage instead of
TJvGIFImage, you can unregister with:
UnregisterGraphicSignature('GIF', 0);
or just
UnregisterGraphicSignature(TJvGIFImage); // must add JvGIF unit in uses clause
then:
RegisterGraphicSignature('GIF', 0, TGIFImage); // must add GIFImage to uses clause
I follow the instruction and Added GIFImage in the uses clause at that unit. Also, in procedure GraphicSignaturesNeeded I added :
RegisterGraphicSignature('GIF', 0, TGIFImage);
RegisterGraphicSignature([$4D, $4d, 0, $2A], 0, TWICImage); // TIFF
RegisterGraphicSignature([$49, $49, $2A, 0], 0, TWICImage); // TIFF
The TIFF info is based on
Tip: detecting graphic formats
Then I used the makemodified.bat to re-compile JVCL.
Before the change, loading image to the TJvDBImage will load the file and give endless error of "bitmap image not valid". After change, it refuse to load the file and give the same error for 1 time.
If I load GIF / TIFF image to the field using other tools, when displaying , it give endless error mentioned above. If I load the field content using the above link functions, it can display in a TImage perfectly.
So, what have I missed or doing wrong?
Thank you!

Resource png not found

i make a png resource file named glyfs.rc
GLYF_CONFEDITOR RCDATA confeditor.png GLYF_EXTRAFE RCDATA extrafe.png .......
i add it in my project.
Put a TsBitBtn (it is an alphaskin component class) and try to load the glyf from resource file
sbitbtn1.Glyph.LoadFromResourceName(HInstance,'GLYF_CONFEDITOR');
when i run i get the error resource file with name 'GLYF_CONFEDITOR' not found.
What am i doing wrong?
The 3 party tool i use for png is pngdelphi downloaded from here
But the sbitbtn loads native the png files...
There are multiple problems with your code:
TBitBtn.Glyph is of type TBitmap and TBitmap.LoadFromResourceName assumes RT_BITMAP resource type, not RT_RCDATA, hence "resource not found" error.
Even if you use RT_BITMAP it will throw EInvalidGraphic or similar¹ because again - TBitBtn.Glyph is TBitmap and naturally TBitmap wont load PNG data.
¹ there is WinAPI function LoadImage behind LoadFromResourceName, actual error message may vary.

Resources