canvas教程

将canvas对象画的图象保存为bmp文件。

字号+ 作者:H5之家 来源:H5之家 2017-06-30 16:04 我要评论( )

如何做?..

  • Canvas好像没有提供这些方法.(没找到)
    不过可以用Image控件的Canvas画图.
    然后image1.Picture.SaveToFile试试看

      

  • procedure TForm1.SavePicClick(Sender: TObject);
    var
      i:Integer;
      str:string;
    begin
      for i:=1 to 100  do
      begin
      if FileExists(str) then  else
          begin
           str:='D:\Borland\Delphi7\Projects\mychart\pic'+inttostr(i)+'.bmp';
           Image1.Picture.SaveToFile(str);
           str:='D:\Borland\Delphi7\Projects\mychart\pic'+inttostr(i+1)+'.bmp';
           Image2.Picture.SaveToFile(str);
           exit;
          end;
      end;end;

      

  • 提供一个这样的函数给你参考:procedure TFrmDesktop.FixSize(const vFileName: String);
    var
      vBitmap3: TBitmap;
      vImage: TImage;
    //  DC2, vDC: HDC;
    begin
      try
        vImage := TImage.Create(self);
        vImage.Visible := False;
        vImage.AutoSize := true;
        vBitmap3 := TBitmap.Create;
    //    vBitmap3.Width := 800;
    //    vBitmap3.Height := 600;
        if CompareText(vExt, 'Bmp') = 0 then vImage.Picture.LoadFromFile(OpenDialog1.FileName);
        if CompareText(vExt, 'Jpg') = 0 then vImage.Picture.LoadFromFile(vFileName);
        if vImage.Picture.Bitmap.Height / vImage.Picture.Bitmap.Width > 0.75 then
        begin
          if vImage.Picture.Bitmap.Height >= ScreenY then
          begin
            vImage.Canvas.StretchDraw(Rect(0,0,round(vImage.Picture.Bitmap.Width * ScreenY/vImage.Picture.Bitmap.Height),ScreenY),vImage.Picture.Bitmap);
            vImage.Center := True;
            vBitmap3.Width := round(vImage.Picture.Bitmap.Width * ScreenY/vImage.Picture.Bitmap.Height);
            vBitmap3.Height := ScreenY;
      //    StretchBlt(vDC, 0, 0, 800, 600, DC2, Image2.Left, Image2.Top, Image2.Width, Image2.Height, SRCCOPY);
        //  vImage.Canvas.CopyRect(Rect(0,0, vImage.Width,vImage.Height), Image2.Canvas, Rect(0,0,Image2.Width,Image2.Height));
            vBitmap3.Canvas.CopyRect(Rect(0,0,vBitmap3.Width,vBitmap3.Height), vImage.Canvas, Rect(0,0,round(vImage.Picture.Bitmap.Width * ScreenY/vImage.Picture.Bitmap.Height),ScreenY));
      //    vImage.Picture.SaveToFile('E:\My Documents\My Pictures\Wally.bmp');
    //    vBitmap3.Canvas.CopyRect(Rect(0,0,vBitmap3.Width,vBitmap3.Height), Image2.Canvas, Rect(0,0,Image2.Width,Image2.Height));
        //    if StretchBlt(vDC, 0, 0, 800, 600, DC2, Image2.Left, Image2.Top, Image2.Width, Image2.Height, SRCCOPY) then
    //    BitBlt(vDC, 0, 0, 800, 600, DC2, Image2.Width, Image2.Height, SRCCOPY);
    //    Image2.Picture.Graphic.SaveToFile('E:\My Documents\My Pictures\Wally.bmp');
            vBitmap3.SaveToFile(vFileName);
          end
          else
            vImage.Picture.Bitmap.SaveToFile(vFileName);
        end
        else if vImage.Picture.Bitmap.Height / vImage.Picture.Bitmap.Width < 0.75 then
        begin
          if vImage.Picture.Bitmap.Width >= ScreenX then
          begin
            vImage.Canvas.StretchDraw(Rect(0,0,ScreenX,round(vImage.Picture.Bitmap.Height * ScreenX/vImage.Picture.Bitmap.Width)),vImage.Picture.Bitmap);
            vImage.Center := True;
            vBitmap3.Height := round(vImage.Picture.Bitmap.Height * ScreenX/vImage.Picture.Bitmap.Width);
            vBitmap3.Width := ScreenX;
            vBitmap3.Canvas.CopyRect(Rect(0,0,vBitmap3.Width,vBitmap3.Height), vImage.Canvas, Rect(0, 0, ScreenX, round(vImage.Picture.Bitmap.Height * ScreenX/vImage.Picture.Bitmap.Width)));
            vBitmap3.SaveToFile(vFileName);
          end
          else
            vImage.Picture.Bitmap.SaveToFile(vFileName);
        end
        else
        begin
          if vImage.Picture.Bitmap.Height >= ScreenY then
          begin
            vImage.Canvas.StretchDraw(Rect(0,0,ScreenX,ScreenY),vImage.Picture.Bitmap);
            vImage.Center := True;
            vBitmap3.Width := ScreenX;
            vBitmap3.Height := ScreenY;
            vBitmap3.Canvas.CopyRect(Rect(0,0,vBitmap3.Width,vBitmap3.Height), vImage.Canvas, Rect(0,0,ScreenX,ScreenY));
            vBitmap3.SaveToFile(vFileName);
          end
          else
            vImage.Picture.Bitmap.SaveToFile(vFileName);
        end;
      finally
        vImage.free;
      end;
    end;

      

     

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • French Connection MILI CANVAS 女士连帽风衣$40(约¥326)

      French Connection MILI CANVAS 女士连帽风衣$40(约¥326)

      2017-07-01 10:05

    • h5 canvas 图片上传操作

      h5 canvas 图片上传操作

      2017-06-30 13:00

    • JavaScript html5 canvas画布中删除一个块区域的方法

      JavaScript html5 canvas画布中删除一个块区域的方法

      2017-06-30 12:05

    • windows phone (25) Canvas元素B

      windows phone (25) Canvas元素B

      2017-06-29 16:02

    网友点评