TGPBitmap转为TBitmap

来源:互联网 发布:淘宝企业店铺经营范围 编辑:程序博客网 时间:2024/05/16 14:21
TGBBitmap用在GDI+中,而TBitmap用在GDI中,有的时候,我们需要将它们进行转换,例如我们要将一个JPEG的图片的缩略图放到TBitmap中,这样它就可以很方便的被Windows中的多个控件所使用(如ImageList、Image等等控件)。
 
       程序用DELPHI实现,使用了for Delphi 的GDI+接口函数,GDI+部分的代码可能与网上的不同,但差别不大。
 
     代码如下:
 
use  gdipapi,gdipobj,gdiputil
 
procedure TForm1.ToBitmap(fnames:string);
VAR Graphics  : TGPGraphics;
    Image, Thumbnail: TGPImage;
    TGPbmp:TGPBitmap;
    bmp:TBitmap;
    hb:HBitmap;
begin
   //打开源图片文件,可以是JPEG、BMP、GIF、TIFF、PNG
   Image:=TGPImage.Create(fnames);
   //创建一个120*120的TGPBitmap,作为绘制缩略图的容器
   TGPbmp:=TGPBitmap.Create(120,120,PixelFormat32bppRGB);
   //取得源图片的缩略图
   Thumbnail := image.GetThumbnailImage(120,120,  nil,  nil);
   //绘制缩略图到TGBbmp;
   Graphics :=TGPGraphics.Create(TGPbmp);
   Graphics.DrawImage(Thumbnail, 0,0,Thumbnail.GetWidth,Thumbnail.GetHeight);
   //创建TBitmap位图
   bmp:=Tbitmap.Create ;
   bmp.width:=120;
   bmp.height:=120;
   //开始转换,hb用于过渡。
   TGBbmp.GetHBITMAP(0,hB);
   bmp.handle:=hb;
   
   {你自己的代码,将bmp用在合适的地方}
   {例如 imagelist1.add(bmp,nil);}
   
   //使用完了,释放.
   Image.free;
   Thumbnail.free;
   Graphics.free;
   TGPbmp.free;
   bmp.free;
 
end;

 反转换

function BmpToGPBmp(bmp: TBitmap;alpha:Byte): TGPBitmap;
var
  x, y: Integer;
  p0, p1: pbytearray;
  isXPIcon:boolean;
  ScanLines:array of Byte;
  W, H: integer;
  Data: TBitmapData;
  CurrentX:integer;
begin
  bmp.PixelFormat := pf32bit;
  try
    SetLength(ScanLines, Bmp.Height * Bmp.Width * 4);
    for y := 0 to bmp.Height - 1 do
    begin
      p0 := bmp.scanline[y];
      CurrentX := bmp.Width * y * 4;
      for x := 0 to bmp.Width - 1 do
      begin
        ScanLines[CurrentX + x * 4] := p0[x * 4];
        ScanLines[CurrentX + x * 4 +1] := p0[x * 4 +1];
        ScanLines[CurrentX + x * 4 +2] := p0[x * 4 +2];
        //设置Alpha
        ScanLines[CurrentX + x * 4 +3] := alpha; // p0[x * 4 + 3];
      end;
    end;
 
    Result := TGPBitmap.Create(bmp.Width, bmp.Height);//(bmp.Handle, bmp.Palette); //  ;//
    W := Result.GetWidth;
    H := Result.GetHeight;
    Result.LockBits(MakeRect(0,0, W, H), ImageLockModeRead or ImageLockModeWrite, PixelFormat32bppARGB, Data);
    Move(ScanLines[0], Data.Scan0^, Data.Height * Data.Stride);
    Result.UnlockBits(Data);
  finally
    SetLength(ScanLines,0);
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  bmp: TBitmap;
  g: TGPGraphics;
  img: TGPBitmap;
begin
  bmp := TBitmap.Create;
  bmp.LoadFromFile('c:\a.bmp');
  img := BmpToGPBmp(bmp,150);
  g := TGPGraphics.Create(Canvas.Handle);
  try
    g.DrawImage(img,0,0,img.GetWidth ,img.GetHeight );
  finally
    FreeAndNil(g);
    FreeAndNil(img);
    FreeAndNil(bmp);
  end;
end;
0 0
原创粉丝点击