Click here to Skip to main content
Licence 
First Posted 12 Oct 2003
Views 66,521
Bookmarked 36 times

Fading an Image Away

By | 12 Oct 2003 | Article
Fading an Image Away

Introduction

While working on my new project (chronology control) I needed the ability to differentiate between more and less important parts of data. My idea was to put more important parts of data to the center of screen and have less important part fade away.

Using double buffering I would draw the visible part of the control to bitmap and then fade it proportional to the distance from the center.

Surprisingly enough, I didn't find a quick and simple solution to the problem of gradually fading away the bitmap, anywhere on the net. Thus disregarding the danger of my short-n-cute article being stigmatized as "yet another LinearGradientBrush derivate", I decided to share this neat trick with you.

How to Fade an Image Away

The solution to this problem is painting a simple transparent gradient over the image. I use linear gradient in the demo, but you could use path gradient to achieve more spectacular results. This gradient must progress from completely transparent black color - this will give the important part of your picture less vague appearance with natural colors - towards less transparent color of the background.

Technically, from alpha 0 and RGB(0,0,0) to alpha 255 and the background color.

Following code does it all:

// Preconditions: image, graphics and backColor initialized
Rectangle imageRect=new Rectangle(0,0,image.Width,image.Height);
// Place it on the window.
graphics.DrawImage(image,imageRect);
// Create gradient brush from black to window's back color.
LinearGradientBrush brush=new
LinearGradientBrush(new Rectangle(0,0,image.Width,image.Height),
    Color.FromArgb(0,0,0,0),
    backColor,
    0,false);
// And paint it over image...
e.Graphics.FillRectangle(brush,imageRect);
// ...voila!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Tomaž Štih

Chief Technology Officer

Slovenia Slovenia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalrecognition Pinmemberamir mortazavi18:37 26 Jan '05  
Generalconver to c++ PinmemberLior Shoval6:21 18 Oct '03  
GeneralRe: conver to c++ Pinmembertstih1:09 22 Oct '03  
Generalnew interesting job PinmemberEgidio0:48 13 Oct '03  
GeneralRe: new interesting job Pinmemberthebeekeeper13:00 21 Feb '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 13 Oct 2003
Article Copyright 2003 by Tomaž Štih
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid