Title: Image can show preview-image in dwg file
Question: i have write a component from image which can show the preview-image in dwg file
Answer:
unit DWGView;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type BITMAPINFO256 = RECORD
bmiHeader: BITMAPINFOHEADER;
bmiColors: Array[0..255] of RGBQUAD;
end;
type
TNoPreviewEvent = procedure(Sender: TOBject) of Object;
TFileErrorEvent = procedure(Sender: TOBject; DWGName: String) of Object;
TDWGView = class(TImage)
private
FDWGVersion: String;
FDWGFile: String;
FNoPreviewEvent: TNoPreviewEvent;
FOnFileError: TFileErrorEvent;
FImage: TImage;
procedure SetDWGFile(const Value: String);
procedure SetFImage(const Value: TImage);
{ Private declarations }
protected
procedure ReadDWG;
constructor TDWGView;
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
property Image: TImage read FImage write SetFImage;
property DWGFile: String read FDWGFile write SetDWGFile;
property DWGVersion: String read FDWGVersion;
property OnNoPreview : TNoPreviewEvent read FNoPreviewEvent write FNoPreviewEvent;
property OnFileError: TFileErrorEvent read FOnFileError write FOnFileError;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Voice', [TDWGView]);
end;
procedure TDWGView.ReadDWG;
Var
DWGF: TFileStream; // ??
MemF: TMemoryStream; //
BMPF: TMemoryStream; // ¦Ë??
SentinelF: TMemoryStream; //? 16?
bif: BITMAPINFO256; // ¦Ë???
bfh: BITMAPFILEHEADER; // ¦Ë???? 14?
PosSentinel: LongInt; // ?¦Ë
LenPreview: Integer; // ???????
RasterPreview: ShortInt; // ??????
// 0 ??? 1 BMP??
// 2 WMF?? 3 ??BMPWMF??
PosBMP: Integer; // ???¦Ë??¦Ë?
LenBMP: Integer; // ???BITMAPFILEHEADER??¦Ë?
IndexPreview: Integer;
TypePreview: Shortint; // ??
begin
if Assigned(FOnFileError) then
FOnFileError(Self,FDWGFile);
DWGF := TFileStream.Create(FDWGFile,fmOpenRead);
BMPF := TMemoryStream.Create;
MemF := TMemoryStream.Create;
SentinelF := TMemoryStream.Create;
try
SetLength(FDWGVersion,6);
DWGF.ReadBuffer(FDWGVersion[1],6);
DWGF.Position := 13; // ??13??
DWGF.Read(PosSentinel, 4);
DWGF.Position := PosSentinel;
SentinelF.CopyFrom(DWGF,16); // ??
DWGF.Read(LenPreview,4); // ?
DWGF.Read(RasterPreview,1); // ???
for IndexPreview := RasterPreview -1 downto 0 do
begin
MemF.Position := 0;
MemF.CopyFrom(DWGF,9); // ?? 9?
MemF.Position := 0;
MemF.Read(TypePreview,1); // TypePreview ??
case TypePreview of
1: ; // ?¦Å
2: begin
// BMP??,DWG?§ÒBMP??¦ÊBMPœZ
// ????BITMAPFILEHEADER
MemF.Position := 1;
MemF.Read(PosBMP,4); // 2,5
MemF.Read(LenBMP,4); // 6,9
DWGF.Position := PosBMP;
DWGF.ReadBuffer( bif,sizeof(bif));
with bif do
begin
bmiColors[0].rgbBlue := 0;
bmiColors[0].rgbGreen := 0;
bmiColors[0].rgbRed := 0;
bmiColors[225].rgbBlue := 255;
bmiColors[225].rgbGreen := 255;
bmiColors[225].rgbRed := 255;
end;
bfh.bfType := $4D42;
bfh.bfSize := LenBMP + sizeof(bfh); //
bfh.bfReserved1 := 0;
bfh.bfReserved2 := 0;
bfh.bfOffBits := 14 + $28 + 1024;
BMPF.Position := 0;
BMPF.Write(bfh,sizeof(bfh));
BMPF.WriteBuffer(bif,sizeof(bif));
BMPF.CopyFrom(DWGF,LenBMP - 1064);
BMPF.Position := 0;
Picture.Bitmap.LoadFromStream(BMPF);
end;
3: ; // WMF?22??Aldus??
end;
end;
finally
SentinelF.Free;
MemF.Free;
DWGF.Free;
BMPF.Free;
end;
end;
procedure TDWGView.SetDWGFile(const Value: String);
begin
FDWGFile := Value;
ReadDWG;
end;
procedure TDWGView.SetFImage(const Value: TImage);
begin
FImage := Value;
end;
constructor TDWGView.TDWGView;
begin
//TODO: Add your source code here
FDWGFile := '';
FDWGVersion := '';
end;
end.