Clear timage.canvas in delphi 7 - delphi

How to clear timage canvas to avoid duplicate image when changing input size? Why nil command doesn't work?
This is my code
begin
image1.Canvas := nil;
image1.Canvas.Pen.Color := clRed;
image1.Canvas.Brush.Color := clBlue;
image1.canvas.rectangle(10,10,vwpj,vwlb);
end;

You can't assign Nil or any value to Canvas, Canvas is a property for read only, so you need to remove the first line and then draw on the TImage canvas:
Image1.Canvas.Pen.Color := clRed;
Image1.Canvas.Brush.Color := clBlue;
Image1.canvas.rectangle(0,0,Image1.Height,Image1.Width);
Edit:
You have to set the image to default every time you draw on it's canvas:
Procedure:
Procedure TForm1.Default(Image: TImage);
begin
Image.Canvas.Pen.Color := clBtnFace;
Image.Canvas.Brush.Color := clBtnFace;
Image.Canvas.FillRect(Rect(0,0,Image.Height,Image.Width));
end;
Then call it as:
procedure TForm1.Button1Click(Sender: TObject);
begin
Default(Image1);
Image1.Canvas.Pen.Color := clRed;
Image1.Canvas.Brush.Color := clBlue;
Image1.canvas.rectangle(0,0,Image1.Height,Image1.Width);
end;

From what you have written and tried to explain in your many previous edits.
This is a possible solution to your problem.
Requirements:
Add four TEdit components to your form.
Add one TButton.
And one TImage.
Code:
var
Xorigin,Yorigin,vwpj,vwlb:integer;
....
begin
vwpj := strtoint(vwpjEdit.text);
vwlb := strtoint(vwlbEdit.text);
Xorigin := strtoint(XoriginEdit.Text);
Yorigin := strtoint(YoriginEdit.Text);
// You have to wipe the canvas with a base color,
image1.Canvas.Brush.Color := clwhite;
image1.Canvas.FillRect(rect(0,0,image1.Width,image1.height));
image1.Canvas.Pen.Color := clRed;
image1.Canvas.Brush.Color := clBlue;
image1.Canvas.rectangle(Xorigin,Yorigin,vwlb,vwpj);
end;
Explanation: I understand that you want to draw a rectangle on the Canvas property of a TImage. With the condition of each time you resize the rectangle you want to clear the Canvas (you implied this by assigning nil to canvas which is wrong considering that Canvas is a read only property).
Now the above code does this by filling the canvas with a base color (I chose clwhite) by using the Fillrect() method.
From this you need to understand that there is no such thing as clearing the image, either you delete it (using the free command as you say) and it will be gone and if you want to draw on it again you will need to create it.
the second option is that you fill it with a background color (the base clwhite I chose) or as a third option, resize the image as well.
All what maters is that as long as that image is still there the canvas and what you have drawn on it will remain.
Results of the code above

Related

How can I change the icon/item size of a TListView in vsIcon ViewStyle?

I need a control which can display thumbnails, for this I thought TListView with the ViewStyle set as vsIcon would be good enough for my purposes, unfortunately I realised that TImageList only supports images up to 256x256 in size. I know there are 3rd party solutions for this but I had hoped to work with the standard TListView.
The images I need to display are approximately 348x480 so I cannot add them to a imagelist and assign it to a listview.
So then I thought maybe I could store my images in a TList and then ownerdraw the listview, it is quite simple really just by using the CustomDrawItem method and working with the Item.DisplayRect to know exactly where to draw to, something like this (quick example):
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
ItemRect: TRect;
IconRect: TRect;
CaptionRect: TRect;
begin
DefaultDraw := False;
ItemRect := Item.DisplayRect(drBounds);
IconRect := Item.DisplayRect(drIcon);
CaptionRect := Item.DisplayRect(drLabel);
with ListView1 do
begin
if cdsHot in State then
begin
Canvas.Brush.Style := bsSolid;
Canvas.Brush.Color := clSkyBlue;
Canvas.FillRect(ItemRect);
end;
if cdsSelected in State then
begin
Canvas.Brush.Style := bsSolid;
Canvas.Brush.Color := clBlue;
Canvas.FillRect(ItemRect);
end;
{ my picture list is a custom control that holds bitmaps in a TList }
if MyPictureList1.Items.Count > 0 then
MyPictureList1.Draw(Canvas, IconRect.Left + 348 div 4, IconRect.Top + 2, Item.ImageIndex);
// commented out old code drawing from imagelist
{ if LargeImages <> nil then
begin
LargeImages.Draw(Canvas, IconRect.Left + LargeImages.Width div 4, 2, Item.ImageIndex);
end; }
// draw text etc
end;
end;
The problem is how to change the size of each listview item? Typically setting the imagelist will change the size of the items, but I cannot use an imagelist because of the size limitations.
I had tried ListView_SetIconSpacing(ListView1.Handle, 348, 480); which did not seem to do anything, I also tried inflating the local rects I assigned but no luck there.
Is it possible to manually set the icon/item size of a listview to be greater than 256px and if so, how could I achieve this?

Canvas.textout doesn´t show text after a new series is made visible

So what i'm doing is display the x and y values of the mouse pointer on a teechart chart using the following code, inside the onmousemove event:
oscilografia.Repaint;
if ((x>236) and (x<927)) and ((y>42) and (y<424)) then
begin
oscilografia.Canvas.Brush.Style := bsSolid;
oscilografia.Canvas.Pen.Color := clBlack;
oscilografia.Canvas.Brush.Color := clWhite;
oscilografia.Canvas.TextOut(x+10,y,datetimetostr(oscilografia.Series[0].XScreenToValue(x))+','+FormatFloat('#0.00',oscilografia.series[0].YScreenToValue(y)));
edit1.Text:=inttostr(x)+' '+inttostr(y);
end;
The code works fine, but a problem happens when i make another series visible by selecting it on the legend: the text inside the box created by canvas.textout isn´t shown anymore.
The box is still there following the mouse, but without any text. So i would like a solution to this.
The basic problem is down to how painting works. Windows do not have persistent drawing surfaces. What you paint onto a window will be overwritten the next time the system needs to repaint it.
You need to arrange that all painting is in response to WM_PAINT messages. In Delphi terms that typically means that you would put your painting code in an overridden Paint method.
So the basic process goes like this:
Derive a sub-class of the chart control and in that class override Paint. Call the inherited Paint method and then execute your code to display the desired text.
In your OnMouseMove event handler, if you detect that the mouse coordinates text needs to be updated, call Invalidate on the chart.
The call to Invalidate will mark that window as being dirty and when the next paint cycle occurs, your code in Paint will be executed.
What is more, when anything else occurs that forces a paint cycle, for instance other modifications to the chart, your paint code will execute again.
Note, as an alternative to sub-classing, you can probably use the TChart event OnAfterDraw. But I'm not an expert on TChart, so am not sure. The main points though are as I state above.
From a comment you wrote, I see you followed this example.
Note it doesn't draw any rectangle; it only draws text, so I'm not sure to understand what box is following your mouse.
Also note the example calls Invalidate, as David Heffernan suggested in his answer.
Find below a modified version of the same example, painting a rectangle before the text.
procedure TForm1.FormCreate(Sender: TObject);
begin
Series1.FillSampleValues(10);
Chart1.View3D := False;
end;
procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var tmpL,tmpL2,ClickedValue : Integer;
tmpWidth, tmpHeight: Integer;
tmpText: string;
begin
clickedvalue := -1;
tmpL2:= -1;
With Chart1 do
begin
If (Series1.Clicked(X, Y) <> -1) And (not OnSeriesPoint) Then
begin
Canvas.Brush.Style := bsSolid;
Canvas.Pen.Color := clBlack;
Canvas.Brush.Color := clWhite;
tmpText:=FormatFloat('#.00',Series1.XScreenToValue(x))+','+FormatFloat('#.00',Series1.YScreenToValue(y));
tmpWidth:=Canvas.TextWidth(tmpText)+10;
tmpHeight:=Canvas.TextHeight(tmpText);
Canvas.Rectangle(x+5, y, x+tmpWidth, y+tmpHeight);
Canvas.TextOut(x+10,y,tmpText);
OnSeriesPoint := True;
ClickedValue:= Series1.Clicked(x,y);
End;
//Repaint Chart to clear Textoutputted Mark
If (ClickedValue=-1) And (OnSeriesPoint) Then
begin
OnSeriesPoint := False;
Invalidate;
End;
tmpL := Chart1.Legend.Clicked(X, Y);
If (tmpL <> -1) And ((tmpL <> tmpL2) Or (not OnLegendPoint)) Then
begin
repaint;
Canvas.Brush.Color := Series1.LegendItemColor(tmpL);
Canvas.Rectangle( X, Y, X + 20, Y + 20);
Canvas.Brush.Color := clWhite;
Canvas.TextOut(x+15,y+7,FormatFloat('#.00',Series1.XValues.Items[Series1.LegendToValueIndex(tmpl)]));
tmpL2 := tmpL;
OnLegendPoint := True;
End;
If (tmpL2 = -1) And (OnLegendPoint) Then
begin
OnLegendPoint := False;
Invalidate;
End;
End;
End;

How do I make a bitmap version of a WMF file that is loaded into a TImage.Picture and move that to a TSpeedButton.Glyph

For the sake of a minimal complete question, I have a WMF file loaded into a TImage control on a form. This control contains the property Picture, which is a TPicture type. I am trying to "rasterize" the WMF file that I loaded into the TImage, and store that into a TSpeedButton.Glyph.
What is interesting about this process is I am able to use this technique to create a resolution independent custom control (a button in my case) that will redraw its glyph for any resolution you like.
In real world usage, I would not have a TImage or a TSpeedButton, but this question is fundamentally about the process of moving content from TPicture to a TBitmap.
Here is the relevant semi-working code:
procedure CopyBitmap( Source:TImage; DestSpeedButton:TSpeedButton );
var
bmp: TBitmap;
begin
bmp:=TBitmap.Create;
try
// note: with WMF loaded, Source.Picture.Bitmap.Width and Height are 0.
bmp.Width := Source.Width; // originally I had Source.Picture.Bitmap.Width, which didn't work.
bmp.Height := Source.Height; //because Source.Picture.Bitmap.Height, doesn't work.
bmp.Canvas.Draw(0,0, Source.Picture.Graphic );
DestSpeedButton.Glyph:=bmp;
finally
bmp.Free;
end;
end;
Is this the correct approach? Why does the image invert during copy?
A sample WMF file, the exact file I'm using, is found here.
Thanks David, for suggesting that I draw the background. This works.
Note that in production I would change the code below to use Vcl.GraphUtils helper called ScaleImage as the results are much prettier. See the second code sample.
// Quick and Dirty : No sub-pixel anti-aliasing.
// Also does not modifies Source, so set Source's size before you
// call this.
procedure CopyBitmap( Source:TImage; DestSpeedButton:TSpeedButton );
var
bmp: TBitmap;
begin
bmp:=TBitmap.Create;
try
bmp.SetSize( Source.Width, Source.Height);
bmp.Canvas.Pen.Style := psClear;
bmp.Canvas.Brush.Style := bsSolid;
bmp.Canvas.Brush.Color := clFuchsia;
bmp.Canvas.Rectangle(0,0, Source.Width+1,Source.Height+1 );
bmp.Canvas.Draw(0,0, Source.Picture.Graphic );
bmp.TransparentColor := clFuchsia;
DestSpeedButton.Glyph:=bmp;
finally
bmp.Free;
end;
end;
Alternative that uses more memory, and is using the TPicture type instead of TImage because in real use I don't even have a TImage just a TPicture, also this looks nicer. Note that it is written around some custom control of my own design (or yours) that has some property type TBitmap. You have to substitute your own controls, or change TMyControlWithAGlyph to TSpeedButton if that's what you want to do:
// A Bit Better Looking. Uses Vcl.GraphUtils function ScaleImage
procedure CopyBitmap( Source:TPicture;
Dest:TMyControlWithAGlyph;
DestType:TCopyDestTypeEnum;
AWidth,AHeight:Integer;
DoInvert:Boolean;
TransparentColor:TColor=clFuchsia );
var
bmp,bmpFullSize: TBitmap;
ARect:TRect;
ScaleAmount:Double;
begin
if not Assigned(Source) then
exit;
if not Assigned(Dest) then
exit;
if not Assigned(Source.Graphic) then
exit;
bmp:=TBitmap.Create;
bmpFullSize := TBitmap.Create;
try
bmpFullSize.SetSize( Source.Width, Source.Height );
bmpFullSize.PixelFormat := pf24bit;
bmpFullSize.Canvas.Pen.Style := psClear;
bmpFullSize.Canvas.Brush.Style := bsSolid;
bmpFullSize.Canvas.Brush.Color := TransparentColor;
bmpFullSize.Canvas.Rectangle(0,0, Source.Width+1,Source.Height+1 );
bmpFullSize.Canvas.Draw(0,0, Source.Graphic );
bmp.SetSize( AWidth, AHeight);
bmp.PixelFormat := pf24bit;
// Vcl.GraphiUtil version needs a floating point scale.
ScaleAmount := AWidth / Source.Width;
ScaleImage(bmpFullSize,bmp,ScaleAmount );
// This lets me have a white icon and turn it black if I want to
// or vice versa
if DoInvert then
InvertBitmap(bmp);
if DestType=DestLargeGlyph then
begin
Dest.LargeGlyph := bmp;
end
else
begin
Dest.Glyph:=bmp;
end;
finally
bmp.Free;
bmpFullSize.Free;
end;
end;
The above code also calls this little helper:
function InvertBitmap(ABitmap: TBitmap): TBitmap;
var
x, y: Integer;
ByteArray: PByteArray;
begin
ABitmap.PixelFormat := pf24Bit;
for y := 0 to ABitmap.Height - 1 do
begin
ByteArray := ABitmap.ScanLine[y];
for x := 0 to ABitmap.Width * 3 - 1 do
begin
ByteArray[x] := 255 - ByteArray[x];
end;
end;
Result := ABitmap;
end;

Add stretched image to ImageList in Delphi

I have a table contains Image in a Picture field and I am going to put them into an ImageList.
Here is the code:
ImageList.Clear;
ItemsDts.First;
ImageBitmap:= TBitmap.Create;
try
while not ItemsDts.Eof do
begin
if not ItemsDtsPicture.IsNull then
begin
ItemsDtsPicture.SaveToFile(TempFileBitmap);
ImageBitmap.LoadFromFile(TempFileBitmap);
ImageList.Add(ImageBitmap, nil);
end;
ItemsDts.Next;
end;
finally
ImageBitmap.Free;
end;
But I have some problem for images with difference size from ImageList size.
Update:
My problem is that when adding Image larger than ImageList size (32 * 32), for example 100 * 150 It does not appear correctly in a component connected to ImageList (for example in a ListView).
It seems newly added image is not stretched but is Croped. I want new image to be stretched as in ImageList Editor.
I don't know if ImageList provides a property to automatically stretch the image. Unless someone finds some built-in, you can always stretch the image yourself before adding it to the ImageList. And while you're at it, stop using the file-on-disk: use a TMemoryStream instead. Something like this:
var StretchedBMP: TBitmap;
MS: TMemoryStream;
ImageList.Clear;
ItemsDts.First;
StretchedBMP := TBitmap.Create;
try
// Prepare the stretched bmp's size
StretchedBMP.Width := ImageList.Width;
StretchedBMP.Height := ImageList.Height;
// Prepare the memory stream
MS := TMemoryStream.Create;
try
ImageBitmap:= TBitmap.Create;
try
while not ItemsDts.Eof do
begin
if not ItemsDtsPicture.IsNull then
begin
MS.Size := 0;
ItemsDtsPicture.SaveToStream(MS);
MS.Position := 0;
ImageBitmap.LoadFromStream(MS);
// Stretch the image
StretchedBMP.Canvas.StretchDraw(Rect(0, 0, StretchedBmp.Width-1, StretchedBmp.Height-1), ImageBitmap);
ImageList.Add(StretchedBmp, nil);
end;
ItemsDts.Next;
end;
finally MS.Free;
end;
finally StretchedBMP.Free;
end;
finally
ImageBitmap.Free;
end;
PS: I edited your code in the browser's window. I can't guarantee it compiles, but if it doesn't, it should be easy to fix.

see the content of an TImagelist in runtime

is possible invoke in runtime the TImagelist editor to see the contents of my TImagelist?
That editor is a design-time editor and is not available at runtime, but you can draw any of the images saved inside an ImageList on any canvas by calling its Draw method and specifying index of the image which you want to draw. The sample code below draws all images saved inside ImageList1 on Form1 in a vertical list:
var
i : Integer;
begin
for i := 0 to ImageList1.Count-1 do
ImageList1.Draw(Form1.Canvas, 16, 16 + (i * ImageList1.Height),i,True);
end;
You can drop a ListView on some form and do something like this:
var
i: Integer;
li: TListItem;
begin
ListView1.LargeImages := ImageList1;
ListView1.Items.BeginUpdate;
try
for i := 0 to Pred(ImageList1.Count) do
begin
li := ListView1.Items.Add;
li.Caption := Format('Image %d', [i]);
li.ImageIndex := i;
end;
finally
ListView1.Items.EndUpdate;
end;
end;
CodeSite has a pretty code logger. You can use it to dump bitmap objects, and see it in the logger window.
http://www.raize.com/DevTools/CodeSite/Default.asp

Resources