Forum > Graphics

Image background

(1/1)

Roman:
I am opening a picture with

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Form1.Image1.Picture.LoadFromFile(filename);
I wish to see a black background around the picture (there is some default color), but I am obviously too dumb to find how. Where should I look?

Handoko:
Just use something black larger than picture and put it behind the picture, maybe a TShape.

Below is an example using a TShape, all components are created runtime. Drop a button an a form and make sure you put StdCtrls, ExtDlgs, ExtCtrls in the uses clause.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);const  ImageBorder = 5;const  Image:      TImage = nil;  Background: TShape = nil;begin  with TOpenPictureDialog.Create(Form1) do  begin    if Execute then    begin      if not(Assigned(Image)) then      begin        Image                  := TImage.Create(Form1);        Image.Parent           := Form1;        Image.Left             := 50;        Image.Top              := 50;        Background             := TShape.Create(Form1);        Background.Parent      := Form1;        Background.Brush.Color := clBlack;        Background.Pen.Style   := psClear;        Background.Left        := Image.Left - ImageBorder;        Background.Top         := Image.Top  - ImageBorder;        Background.SendToBack;      end;      Image.Picture.LoadFromFile(FileName);      Image.Height      := Image.Picture.Height;      Image.Width       := Image.Picture.Width;      Background.Height := Image.Picture.Height + 2*ImageBorder;      Background.Width  := Image.Picture.Width  + 2*ImageBorder;    end;    Free;  end;end;

* Line #3 is for the thickness of the border.
* Line #24 is to make sure the background behind the image.

Roman:
Thank you. It works.

Handoko:
This below is a different solution, it uses a temporary TPicture:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);const  Border = 5;const  Image: TImage = nil;var  Temp: TPicture;begin  with TOpenPictureDialog.Create(Form1) do  begin    if Execute then    begin      if not(Assigned(Image)) then      begin        Image        := TImage.Create(Form1);        Image.Parent := Form1;        Image.Left   := 50;        Image.Top    := 50;      end;      Temp := TPicture.Create;      Temp.LoadFromFile(FileName);      Image.Height := Temp.Height + 2*Border;      Image.Width  := Temp.Width  + 2*Border;      Image.Picture.Clear;      Image.Canvas.CopyRect(        Rect(Border, Border, Temp.Width + Border, Temp.Height + Border),        Temp.Bitmap.Canvas,        Rect(0, 0, Temp.Width, Temp.Height)        );      Temp.Free;    end;    Free;  end;end;

Navigation

[0] Message Index

Go to full version