CovertBmp.pas
上传用户:dgeyuang
上传日期:2007-01-11
资源大小:65k
文件大小:3k
源码类别:

传真(Fax)编程

开发平台:

Delphi

  1. unit CovertBmp;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5.   Menus, ActnList,jpeg,math,extctrls;
  6. type
  7.    TBMInfo = record
  8.        bmType,                    //Bitmap类型
  9.        bmWidth,                   //图像宽度(像素值)
  10.        bmHeight,                  //图像高度(像素值)
  11.        bmWidthBytes: longint;     //每条扫描线指定的字节数
  12.        bmPlanes,                  //指定颜色位
  13.        bmBitsPixel: word;          //色彩值
  14.      //bmBits;                     //指向位图值地址
  15.      end;
  16. Function CheckBmp(image: TImage;SourceName:string):Integer;
  17. function ConvertToBmp(image1:Timage;DestionName:String):string;
  18. implementation
  19. Function CheckBmp(image: TImage;SourceName:string):Integer;
  20. var
  21.    bmInfo:TBMInfo;
  22.    bmp:TBitmap;
  23.    Ext:String;
  24. begin
  25.   if not FileExists(SourceName) then
  26.   begin
  27.      result:=-1;
  28.      exit;
  29.   end;
  30.   Ext:=LowerCase(ExtractFileExt(SourceName));
  31.   if Ext='.bmp' then
  32.   begin
  33.   Screen.Cursor := crHourGlass;
  34.   try
  35.     image.Picture.LoadFromFile(SourceName);
  36.     Image.top:=0;
  37.     Image.left:=0;
  38.     if Image.Picture.Graphic is TJPEGImage then
  39.     begin
  40.        bmp:=TBitmap.Create;
  41.        bmp.Assign(TJPEGImage(Image.Picture.Graphic));
  42.     end
  43.     else bmp:=Image.Picture.Bitmap;
  44.     GetObject(bmp.Handle,SizeOf(bmInfo),@bmInfo);
  45.     case bmInfo.bmBitsPixel of
  46.        24: result:=24;
  47.        16: result:=16;
  48.     else result:=Trunc(IntPower(2,bmInfo.bmBitsPixel));
  49.     end;
  50.     if(Image.Picture.Graphic is TJPEGImage) then bmp.free;
  51.   finally
  52.     Screen.Cursor := crDefault;
  53.   end;
  54.  end else Result:=-1;
  55. end;
  56. function ConvertToBmp(image1:Timage;DestionName:String):string;
  57. var
  58.    bmp:TBitmap;
  59.    i:integer;
  60.    Picture:TPicture;
  61. begin
  62. //   Picture:= TPicture.Create;
  63.    try
  64.       bmp := TBitmap.Create;
  65.       image1.Picture.LoadFromFile(DestionName);
  66.       Picture:=image1.Picture;
  67.      if ( Picture.Graphic is TBitmap) then
  68.      begin
  69.        with Bmp do
  70.         begin
  71.             Width := Picture.Width;
  72.             Height := Picture.Height;
  73.             PixelFormat := pf8Bit;
  74.             Canvas.Draw(0, 0, Picture.Graphic);
  75.         end;
  76.         Picture.Bitmap.Assign(Bmp);
  77.         i:=Pos('.',DestionName);
  78.          bmp.SaveToFile(Copy(DestionName,1,i-1)+'.Bmp');
  79.      end else if Picture.Graphic is TJPEGImage then
  80.      begin
  81.          bmp:=TBitmap.create;
  82.          bmp.Assign(TJPEGImage(Picture.Graphic));
  83.          bmp.PixelFormat:=pf8bit;
  84.          i:=Pos('.',DestionName);
  85.          bmp.SaveToFile(Copy(DestionName,1,i-1)+'.Bmp');
  86.          result:=Copy(DestionName,1,i-1)+'.Bmp';
  87.          bmp.free;
  88.          result:=Copy(DestionName,1,i-1)+'.Bmp';
  89.     end;
  90.     except
  91.  end;
  92. end;
  93. end.