Recent

Author Topic: Having Problem with GradientFill...  (Read 5056 times)

Josh

  • Hero Member
  • *****
  • Posts: 1344
Having Problem with GradientFill...
« on: May 17, 2018, 01:28:36 pm »
Hi

Banging head against wall for last few hours, and now my head is hurting..

I have minimized the code below; which exhibits the same problem.

If I create a form place a timage and trackbar (set max to image height) on it and add the code below to trackbar change. When I move the trackbar I get a gradient fill on the image, as expected.

Now if I add the same code to my existing project, then it does not work, if i use the rectangle function then It draws..

Anyone had a similar problem, I have checked that the calls are calling the same unit calls; ie thats why I explicitly added classes. prefix.

I suspect if you test the code it will work.. This is expected...

Thanks


Code: Pascal  [Select][+][-]
  1. procedure TForm1.TrackBar1Change(Sender: TObject);
  2. Var TempBmp:TBitmap;
  3.   image_height:integer;
  4.       image_width:integer;
  5. begin
  6.   image_width:=Image1.Width;
  7.   image_height:=Image1.Height;
  8.   TempBmp:=TBitmap.Create;
  9.   TempBmp.Width:=image_width;
  10.   TempBmp.Height:=image_height;
  11.   TempBmp.Canvas.Pen.Style:=psSolid;
  12.   TempBmp.Canvas.pen.Width:=0;
  13.  // TempBmp.canvas.Rectangle(0,0,image_width,TrackBar1.Position);
  14.   TempBmp.Canvas.GradientFill(classes.rect(0,0,image_width,TrackBar1.Position),clBlue,clGreen,gdVertical);
  15.   Image1.Picture.Bitmap:=tempbmp;
  16.   TempBmp.Free;
  17. end;            
  18.  
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Thaddy

  • Hero Member
  • *****
  • Posts: 16177
  • Censorship about opinions does not belong here.
Re: Having Problem with GradientFill...
« Reply #1 on: May 17, 2018, 01:35:26 pm »
It probably needs an explicit repaint message, so add a Application.ProcessMessages or an otherwise forced paint.
If I smell bad code it usually is bad code and that includes my own code.

Josh

  • Hero Member
  • *****
  • Posts: 1344
Re: Having Problem with GradientFill...
« Reply #2 on: May 17, 2018, 01:42:50 pm »
Hi Thaddy,

Thanks for your response, I have tried Image.refresh and application.processmessages.
But this has no effect. The rectangle draws a solid block to canvas and updates without the force update.

It's as though the fillrectangle is not doing anything..
I have even modified it so half is solid and half is gradient; and only the solid part is drawn.

Like I say its odd, as new project with exactly the same code works; but not in existing project; wondering if exiting project unit declaration could be interferring with this.
I may try creating a seperate unit based on new project uses declaration and add the code to that.. Maybe you never know.
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Handoko

  • Hero Member
  • *****
  • Posts: 5376
  • My goal: build my own game engine using Lazarus
Re: Having Problem with GradientFill...
« Reply #3 on: May 17, 2018, 02:13:09 pm »
Yes, your code works. It works correctly without showing any issue, tested on my Linux computer.

So can you provide us the code that can 'really' show the issue?

Josh

  • Hero Member
  • *****
  • Posts: 1344
Re: Having Problem with GradientFill...
« Reply #4 on: May 17, 2018, 02:28:51 pm »
Hi Handoko,

Thanks for checking.

Unfortuantely I cant give the code for when its not working, as it's a very large project and has propritery code.

When the application is doing loads of work, I have various indication of what it is doing at a given time, these were done with rectangle and worked fine, just changing it to fillgradient then I have no indication, taking out this  code and placing in new project using a trackbar to simulate the progress then all is fine.

At the moment I am trying to work out why.
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

balazsszekely

  • Guest
Re: Having Problem with GradientFill...
« Reply #5 on: May 17, 2018, 02:34:19 pm »
The next logical step is to save the bmp to a file:
Code: Pascal  [Select][+][-]
  1. TempBmp.Canvas.GradientFill(classes.rect(0,0,image_width,TrackBar1.Position),clBlue,clGreen,gdVertical);
  2. TempBmp.SaveToFile('thepath');

Check the saved bmp. If is empty insert a Canvas.Changed after the gradient draw.
Code: Pascal  [Select][+][-]
  1. TempBmp.Canvas.GradientFill(classes.rect(0,0,image_width,TrackBar1.Position),clBlue,clGreen,gdVertical);
  2. TempBmp.Canvas.Changed;
« Last Edit: May 17, 2018, 02:43:04 pm by GetMem »

Handoko

  • Hero Member
  • *****
  • Posts: 5376
  • My goal: build my own game engine using Lazarus
Re: Having Problem with GradientFill...
« Reply #6 on: May 17, 2018, 03:15:59 pm »
@josh

I understand, for some reasons you're not willing to publicize the code. But can you write a demo showing that issue? I personally believe the bug is not in the code that you just provided. If I have the case like yours, I will do things like these:

01. Make a backup of the project
02. Remove one unrelated form/unit
03. Compile and run
04. Go to step #02, until only the form and necessary units that showing the issue
05. Remove one unnecessary component on the form
06. Compile and run
07. Go to step #05, until only the necessary component that showing the issue

Now, you get the 'fat loss' version of the project that can show the issue. So you can continue:

08. Try to compile and run the code on different Lazarus/FPC versions
09. Try to compile and run the code on different OSes

If the problem is not reproducible on step #08 or #09, then it should be widgetset/OS-dependent/compiler's issue.

But if the problem still exists, then I will one-by-one replace the forms/units by writing the new ones. Because by regenerating them, I may find some typing or logic mistake that I previous made on those old code.

But if still can't find the answers, I will set lots of debug things to see where goes wrong.

Bugs can hide in anywhere of the code. Debugging a big project is difficult, so to make it easier I usually will do the 'fat loss' first.
« Last Edit: May 17, 2018, 03:26:38 pm by Handoko »

Josh

  • Hero Member
  • *****
  • Posts: 1344
Re: Having Problem with GradientFill...
« Reply #7 on: May 17, 2018, 03:41:54 pm »
@getmem

Thanks for some diag tips, i added in the savetofile, and this shows that it is NOT creating the gradient on the canvas, i added in the canvas.changed before the savetofile and it still does not add the gradient.  using Rectangle fill then all images generated are complete.

So perplexed, as one of my components in this project uses the GradientFill ( no special graphics library) to generate control canvases and these work fine.

@handoko
The project is a single form project; so this does help diagnose; I will strip it down and start removing components and libraries; incase they are interferring. It does use a fair amount of bgrabitmap and bgracontrols;

Might take some time to elimiante, but hopefully can track down what is casuing it.
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

balazsszekely

  • Guest
Re: Having Problem with GradientFill...
« Reply #8 on: May 17, 2018, 03:51:30 pm »
Then you overwrite somehow the drawing or you're writing to the wrong canvas. Unfortunately I cannot help you without the code. Alternatively you can put a breakpoint to each place where you write to the image and use the Call Stack window to trace back the code.

Josh

  • Hero Member
  • *****
  • Posts: 1344
Re: Having Problem with GradientFill...
« Reply #9 on: May 18, 2018, 03:48:05 am »
Hi

I have created a sampple app; this routine is writing to timage canvas, and works for rectangle but not for gradientfill

I suspect its me and something obvious.


Updated zip file 4:05am
« Last Edit: May 18, 2018, 04:05:47 am by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

balazsszekely

  • Guest
Re: Having Problem with GradientFill...
« Reply #10 on: May 18, 2018, 06:25:56 am »
Quote
I have created a sampple app; this routine is writing to timage canvas, and works for rectangle but not for gradientfill
Please test attached project.
« Last Edit: May 18, 2018, 06:50:29 am by GetMem »

Handoko

  • Hero Member
  • *****
  • Posts: 5376
  • My goal: build my own game engine using Lazarus
Re: Having Problem with GradientFill...
« Reply #11 on: May 18, 2018, 07:15:25 am »
Yes, GetMem's code works correctly.

So the culprit is (it is on line #48):
Code: Pascal  [Select][+][-]
  1.     canvas.Pen.Style:=psClear;

Simply remove that line in josh's code, it works correctly on my test.

Josh

  • Hero Member
  • *****
  • Posts: 1344
Re: Having Problem with GradientFill...
« Reply #12 on: May 18, 2018, 08:14:42 pm »
Hi Getman and Handoko,

Thanks for debugging my code and solving my Bad Head ;)
I would not have thought that a pen.style would effect how a GradientFill is drawn; or not in this case.

Again thanks
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

 

TinyPortal © 2005-2018