문제

I have a simple TImage control on a form. I've assigned a Bitmap image at design-time. When running, I read the canvas using TImage.Picture.Bitmap.Canvas. Everything works fine. Then, I load a JPEG image in design-time. However, now when I read this canvas, suddenly the picture disappears.

Why does this happen and how can I avoid it?

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
  Canvas: TCanvas;
begin
  Canvas:= Image1.Picture.Bitmap.Canvas;
  //Do something with Canvas, but canvas is empty and image disappeared
end;
도움이 되었습니까?

해결책

This is expected, since a JPG image simply isn't a bitmap (BMP) image.

You have to convert it to a bitmap in order to do something with it:

var
  bm: TBitmap;
begin
  bm := TBitmap.Create;
  try
    bm.Assign(Image1.Picture.Graphic);
    bm.Canvas.DoSomethingReallyAwesome(ttExtraAwesome);
    bm.SaveToFile('C:\Users\Andreas Rejbrand\Desktop\test5.bmp');
  finally
    bm.Free;
  end;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top