Solved

offscreen bitmaps

Posted on 1998-01-15
4
481 Views
Last Modified: 2010-08-05
I want to construct a line offscreen and then display it over a bitmap with the picture showing through.
I always end up with a white background covering the bitmap.  Why?
eg.,

source:=rect(0,0,600,360);
dest:=rect(0,0,600,360);
offscreen:=tbitmap.create;
try
offscreen.width:=600;
offscreen.height:=360;
with offscreen.canvas do
begin
pen.color:=clred;
brush.color:=clwhite;
brush.style:=bsclear;
moveto(60,60);
lineto(250,300);
end;
paintbox1.canvas.copyrect(dest,offscreen.canvas,source);
finally
offscreen.free;
end;
0
Comment
Question by:nifty
  • 2
4 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1356891
Do you mean that your offscreen bitmap must have a transparent background?  If so, you have to make it draw tranparently onto your other bitmap.  If this is your case, I can give you code to draw transparently.

JB
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1356892
Yeah JimBob, I guess that's what nifty wants.
0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 50 total points
ID: 1356893
If that's the case, then this should be the answer:

const
  TransparentMask = $02000000;

function Min(i1, i2: Integer): Integer;
begin
      if (i1 < i2) then
            Result := i1
      else
            Result := i2;
end;

function Max(i1, i2: Integer): Integer;
begin
      if (i1 > i2) then
            Result := i1
      else
            Result := i2;
end;

function WidthOf(R: TRect): Integer;
begin
      Result := R.Right - R.Left;
end;

function HeightOf(R: TRect): Integer;
begin
      Result := R.Bottom - R.Top;
end;

procedure AdjustRect(var R: TRect; LeftChange, TopChange, RightChange, BottomChange: Integer);
begin
      Inc(R.Left, LeftChange);
      Inc(R.Top, TopChange);
      Inc(R.Right, RightChange);
      Inc(R.Bottom, BottomChange);
end;

procedure DrawTransparentBitmapRect(DC: HDC; Bitmap: HBitmap; xStart, yStart,
  Width, Height: Integer; Rect: TRect; TransparentColor: TColorRef);
var
      BM: Windows.TBitmap;
      cColor: TColorRef;
  bmAndBack, bmAndObject, bmAndMem, bmSave: HBitmap;
  bmBackOld, bmObjectOld, bmMemOld, bmSaveOld: HBitmap;
  hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave: HDC;
  ptSize, ptRealSize, ptBitSize, ptOrigin: TPoint;
begin
  hdcTemp := CreateCompatibleDC(DC);
  SelectObject(hdcTemp, Bitmap);      { Select the bitmap    }
  GetObject(Bitmap, SizeOf(BM), @BM);
  ptRealSize.x := Min(Rect.Right - Rect.Left, BM.bmWidth - Rect.Left);
  ptRealSize.y := Min(Rect.Bottom - Rect.Top, BM.bmHeight - Rect.Top);
  DPtoLP(hdcTemp, ptRealSize, 1);
  ptOrigin.x := Rect.Left;
  ptOrigin.y := Rect.Top;
  DPtoLP(hdcTemp, ptOrigin, 1);       { Convert from device  }
                                      { to logical points    }
  ptBitSize.x := BM.bmWidth;          { Get width of bitmap  }
  ptBitSize.y := BM.bmHeight;         { Get height of bitmap }
  DPtoLP(hdcTemp, ptBitSize, 1);
  if (ptRealSize.x = 0) or (ptRealSize.y = 0) then begin
    ptSize := ptBitSize;
    ptRealSize := ptSize;
  end
  else ptSize := ptRealSize;
  if (Width = 0) or (Height = 0) then begin
    Width := ptSize.x;
    Height := ptSize.y;
  end;

  { Create some DCs to hold temporary data }
  hdcBack   := CreateCompatibleDC(DC);
  hdcObject := CreateCompatibleDC(DC);
  hdcMem    := CreateCompatibleDC(DC);
  hdcSave   := CreateCompatibleDC(DC);
  { Create a bitmap for each DC. DCs are required for a number of }
  { GDI functions                                                 }
  { Monochrome DC }
  bmAndBack   := CreateBitmap(ptSize.x, ptSize.y, 1, 1, nil);
  bmAndObject := CreateBitmap(ptSize.x, ptSize.y, 1, 1, nil);
  bmAndMem    := CreateCompatibleBitmap(DC, Max(ptSize.x, Width), Max(ptSize.y, Height));
  bmSave      := CreateCompatibleBitmap(DC, ptBitSize.x, ptBitSize.y);
  { Each DC must select a bitmap object to store pixel data }
  bmBackOld   := SelectObject(hdcBack, bmAndBack);
  bmObjectOld := SelectObject(hdcObject, bmAndObject);
  bmMemOld    := SelectObject(hdcMem, bmAndMem);
  bmSaveOld   := SelectObject(hdcSave, bmSave);
  { Set proper mapping mode }
  SetMapMode(hdcTemp, GetMapMode(DC));

  { Save the bitmap sent here, because it will be overwritten }
  BitBlt(hdcSave, 0, 0, ptBitSize.x, ptBitSize.y, hdcTemp, 0, 0, SRCCOPY);
  { Set the background color of the source DC to the color,         }
  { contained in the parts of the bitmap that should be transparent }
  cColor := SetBkColor(hdcTemp, TransparentColor);
  { Create the object mask for the bitmap by performing a BitBlt()  }
  { from the source bitmap to a monochrome bitmap                   }
  BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, ptOrigin.x, ptOrigin.y,
    SRCCOPY);
  { Set the background color of the source DC back to the original  }
  { color                                                           }
  SetBkColor(hdcTemp, cColor);
  { Create the inverse of the object mask }
  BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
    NOTSRCCOPY);
  { Copy the background of the main DC to the destination }
  BitBlt(hdcMem, 0, 0, Width, Height, DC, xStart, yStart,
    SRCCOPY);
  { Mask out the places where the bitmap will be placed }
  StretchBlt(hdcMem, 0, 0, Width, Height, hdcObject, 0, 0,
    ptSize.x, ptSize.y, SRCAND);
  {BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);}
  { Mask out the transparent colored pixels on the bitmap }
  BitBlt(hdcTemp, ptOrigin.x, ptOrigin.y, ptSize.x, ptSize.y, hdcBack, 0, 0,
    SRCAND);
  { XOR the bitmap with the background on the destination DC }
  StretchBlt(hdcMem, 0, 0, Width, Height, hdcTemp, ptOrigin.x, ptOrigin.y,
    ptSize.x, ptSize.y, SRCPAINT);
  {BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, ptOrigin.x, ptOrigin.y,
    SRCPAINT);}
  { Copy the destination to the screen }
  BitBlt(DC, xStart, yStart, Max(ptRealSize.x, Width), Max(ptRealSize.y, Height),
    hdcMem, 0, 0, SRCCOPY);
  { Place the original bitmap back into the bitmap sent here }
  BitBlt(hdcTemp, 0, 0, ptBitSize.x, ptBitSize.y, hdcSave, 0, 0, SRCCOPY);

  { Delete the memory bitmaps }
  DeleteObject(SelectObject(hdcBack, bmBackOld));
  DeleteObject(SelectObject(hdcObject, bmObjectOld));
  DeleteObject(SelectObject(hdcMem, bmMemOld));
  DeleteObject(SelectObject(hdcSave, bmSaveOld));
  { Delete the memory DCs }
  DeleteDC(hdcMem);
  DeleteDC(hdcBack);
  DeleteDC(hdcObject);
  DeleteDC(hdcSave);
  DeleteDC(hdcTemp);
end;

procedure InternalDrawTransBmpRect(Dest: TCanvas; X, Y, W, H: Integer; Rect: TRect; Bitmap: TBitmap; TransparentColor: TColor);
var
  MemImage: TBitmap;
  R: TRect;
begin
  MemImage := TBitmap.Create;
  try
    R := Bounds(0, 0, Bitmap.Width, Bitmap.Height);
    if TransparentColor = clNone then begin
                  if (WidthOf(Rect) <> 0) and (HeightOf(Rect) <> 0) then R := Rect;
      MemImage.Width := WidthOf(R);
                  MemImage.Height := HeightOf(R);
      MemImage.Canvas.CopyRect(Bounds(0, 0, MemImage.Width, MemImage.Height),
        Bitmap.Canvas, R);
      if (W = 0) or (H = 0) then Dest.Draw(X, Y, MemImage)
      else Dest.StretchDraw(Bounds(X, Y, W, H), MemImage);
    end
    else  begin
      MemImage.Width := WidthOf(R);
                  MemImage.Height := HeightOf(R);
      MemImage.Canvas.CopyRect(R, Bitmap.Canvas, R);
      DrawTransparentBitmapRect(Dest.Handle, MemImage.Handle, X, Y, W, H,
        Rect, ColorToRGB(TransparentColor and not TransparentMask));
    end;
  finally
    MemImage.Free;
  end;
end;

procedure DrawBitmapTransparent(Dest: TCanvas; XOrigin, YOrigin: Integer; Bitmap: TBitmap; TransparentColor: TColor);
begin
      InternalDrawTransBmpRect(Dest, XOrigin, YOrigin, 0, 0, Rect(0, 0, 0, 0), Bitmap, TransparentColor);
end;



To use this code, suppose you have a TImage on your form called imgTest, with an existing picture inside it:

procedure DoStuff;
var
  Bmp: TBitmap;
begin
  Bmp := TBitmap.Create;
  Bmp.Width := imgTest.Width;
  Bmp.Height := imgTest.Height;
  with Bmp.Canvas do
    begin
      Brush.Color := clYellow;  // This is the transparent color.
      FillRect(Rect(0, 0, imgTest.Width, imgTest.Height));
      Pen.Color := clRed;
      MoveTo(0, 0);
      LineTo(imgTest.Width, imgTest.Height);
    end;
  // Use the same transparent color as above, in this case yellow.
  DrawBitmapTransparent(imgTest.Picture.Bitmap.Canvas, 0, 0, Bmp, clYellow);
  imgTest.Invalidate;
  Bmp.Free;
end;

Regards,
JB
0
 

Author Comment

by:nifty
ID: 1356894
Thanks JB. As you can see I am fairly new at programming (but having a ball all the same)so expect to see postings from me often.I have bought several books and I understand them perfectly
after the 'problem' has been solved, isn't it always the same!
Once again thanks to all at expert exchange.....Nifty.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ddeman not working in activex 3 92
Convert GUI app into console app for Win32 Env 5 78
tidtcpserver connection lost handle 2 69
Adoquery sql  left join does not work 25 80
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now