Picturebox: Change opacity / Decrease brightness?

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 19362 Views - Last Post: 25 May 2011 - 05:45 AM Rate Topic: -----

#1 coderx2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-May 11

Picturebox: Change opacity / Decrease brightness?

Posted 21 May 2011 - 09:53 PM

There is a picturebox containing a .png image.
After a button is clicked, the picture becomes darker.
What's the easiest method to do this?
I thought of using a black picturebox and setting its opacity to 50% and placing it over the image.
But how do I set the opacity? Can it be done?
Is This A Good Question/Topic? 0
  • +

Replies To: Picturebox: Change opacity / Decrease brightness?

#2 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Re: Picturebox: Change opacity / Decrease brightness?

Posted 21 May 2011 - 11:36 PM

Are you allowed to use multiple images, or must it be the one image?
Was This Post Helpful? 0
  • +
  • -

#3 coderx2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-May 11

Re: Picturebox: Change opacity / Decrease brightness?

Posted 22 May 2011 - 12:00 AM

Anything will do
Was This Post Helpful? 0
  • +
  • -

#4 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Picturebox: Change opacity / Decrease brightness?

Posted 22 May 2011 - 12:17 AM

Hi,

You can't use Opacity in a Picturebox or you need to create a picturebox by yourself.

Instead of opacity you can use ColorMatrix.
Look here for more information.

This post has been edited by Luc001: 22 May 2011 - 12:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Re: Picturebox: Change opacity / Decrease brightness?

Posted 22 May 2011 - 02:01 AM

View Postcoderx2, on 22 May 2011 - 12:00 AM, said:

Anything will do


Then simplest way to do it would be to make 3-4 different pictures in PS/GIMP, then have the button coded to change the image. IIRC, you can simply set the "image" property to something different, but don't quote me on that; not at a Windows machine atm, so can't test it out.
Was This Post Helpful? 0
  • +
  • -

#6 coderx2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-May 11

Re: Picturebox: Change opacity / Decrease brightness?

Posted 22 May 2011 - 02:30 AM

Actually I'm coding a game. Creating multiple pictures won't work because there'll be too many to create.

Another problem related to this is:

There are 2 blue circles (.png images) in 2 picturebox.
The outside of the 2 circles are transparent. (i.e. the background of the circle is transparent)
When I place 1 image on the form, the background of the circle is transparent as it should be.
But when I overlap 1 image onto the other, the background of the circle is no longer transparent. i.e. it becomes a white square with a blue circle in the center.

How do I merge the images?
I think there's a complicated way to do it using some graphics api and paint, but can't figure it out.

Please help if you understand what I'm saying and know the solution.
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Picturebox: Change opacity / Decrease brightness?

Posted 22 May 2011 - 04:16 AM

You need to draw the images for all the ones you have onto 1 canvas. You just change the location(point) where they are drawn to.

DrawImage
Was This Post Helpful? 1
  • +
  • -

#8 coderx2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-May 11

Re: Picturebox: Change opacity / Decrease brightness?

Posted 22 May 2011 - 06:33 AM

I used

xForm.xPanel.Refresh()
xForm.xPanel.CreateGraphics.DrawImage(xImage, New Point(xcord, ycord))

in a timer tick event of interval 1 ms, varying the xcord to create an animation.
But the image flickers.

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#9 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Picturebox: Change opacity / Decrease brightness?

Posted 22 May 2011 - 06:50 AM

Hi,

To set up automatic double buffering for a Form, you would use the following line of code in the constructor, after the InitializeComponent method call.

me.SetStyle( 

  ControlStyles.AllPaintingInWmPaint OR _ 

  ControlStyles.UserPaint OR _ 

  ControlStyles.DoubleBuffer,true)


Was This Post Helpful? 0
  • +
  • -

#10 coderx2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-May 11

Re: Picturebox: Change opacity / Decrease brightness?

Posted 22 May 2011 - 08:02 AM

I am drawing in a panel so I did this...

Class DBPanel
    Inherits Panel

    Public Sub New()
        Me.DoubleBuffered = True
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.UserPaint, True)
        Me.SetStyle(ControlStyles.DoubleBuffer, True)
        Me.SetStyle(ControlStyles.ResizeRedraw, True)
        UpdateStyles()
    End Sub
End Class


Is it correct?

Well that didn't work. The drawing still flickers.
Was This Post Helpful? 0
  • +
  • -

#11 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Picturebox: Change opacity / Decrease brightness?

Posted 22 May 2011 - 10:03 AM

Hi,

I think you should do it this way;

Public Class DoubleBufferPanel
	Inherits Panel
	Public Sub New()
		' Set the value of the double-buffering style bits to true.
		Me.SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint, True)

		Me.UpdateStyles()
	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#12 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Picturebox: Change opacity / Decrease brightness?

Posted 22 May 2011 - 10:08 AM

Painting on a picturebox is flicker free. I have even docked one to a form and float controls on it and paint all over the canvas.
Was This Post Helpful? 0
  • +
  • -

#13 coderx2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-May 11

Re: Picturebox: Change opacity / Decrease brightness?

Posted 23 May 2011 - 03:37 AM

Still having the same problem..

Is there a way to create a transparent picturebox?

Public Class TransPB
    Inherits System.Windows.Forms.PictureBox

    Protected Overloads Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
    End Sub
End Class


Tried that.. didn't work
Was This Post Helpful? 0
  • +
  • -

#14 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Picturebox: Change opacity / Decrease brightness?

Posted 23 May 2011 - 03:51 AM

What I was talking about is making 1 large PB the same size as your form and painting the images where they need to go. I think Code Project has a transparent control example.
Was This Post Helpful? 0
  • +
  • -

#15 coderx2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-May 11

Re: Picturebox: Change opacity / Decrease brightness?

Posted 24 May 2011 - 06:52 AM

I found this:

http://www.codeproje...entControl.aspx

But it still doesn't work well as 1 picturebox must be inside the other one.
(which also means that only 2 images can overlap at once.

How do other game programmers do it?

This post has been edited by coderx2: 24 May 2011 - 06:54 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2