How to fade the form image background to gray-scale imitating the XP shutdown screen

sakthivel

New member
Joined
Aug 29, 2006
Messages
2
Programming Experience
Beginner
I want to know how to "How to fade the form image background to gray-scale imitating the XP shutdown screen in windows form?". i heard that this can be achieved by using dimmer dialog. i dont whether .net 2002 or 2003 has this function. Can anyone guide me in coding?
 
Saw an article on this at the code project a while back but it used unmanaged C++. I'm posative that it can be done using managed code and GDI+. It'd be one i'd have to sit down and play with. My basic idea would be to manipulate the image with a matrix. It's no problem converting the image straight to grey-scale with a matrix, it's the fading part that will take a bit of thinking about.
I'll have a go tonight. In the mean time check the Matrix class thats where the root of this particular problem will be solved.
 
Ok, it look like i may have been mistaken here. It would be possible, and i hope that someone smarter than me can figure this out, Here is a function that can make a bitmap greyscale (original source obtained from Planet Source Code) But as for fading it, thats another story. Here's the function...

VB.NET:
Public Shared Function MakeGrayscale(ByRef Bmp As Bitmap) As Bitmap
Dim BmpData As BitmapData
Dim Ptr As System.IntPtr
Dim Red As Integer, Green As Integer, Blue As Integer
Dim x As Integer, y As Integer
Dim Offset As Integer
BmpData = Bmp.LockBits(New Rectangle(0, 0, Bmp.Width, Bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
Ptr = BmpData.Scan0
Offset = BmpData.Stride - Bmp.Width * 3
Dim ColorValue As Byte
For y = 0 To Bmp.Height - 1
For x = 0 To Bmp.Width - 1
Blue = Marshal.ReadByte(Ptr, 0)
Green = Marshal.ReadByte(Ptr, 1)
Red = Marshal.ReadByte(Ptr, 2)
ColorValue = CByte(0.299 * Red + 0.587 * Green + 0.114 * Blue)
Marshal.WriteByte(Ptr, 0, ColorValue)
Marshal.WriteByte(Ptr, 1, ColorValue)
Marshal.WriteByte(Ptr, 2, ColorValue)
Ptr = IntPtr.op_Explicit(Ptr.ToInt32 + 3)
Next
Ptr = IntPtr.op_Explicit(Ptr.ToInt32 + Offset)
Next
Bmp.UnlockBits(BmpData)
Return Bmp
End Function

Here is also a link to a site where a guy who is a lot brighter than me has made it happen, but only in C++. I'm not gonna give up but this is as far as i got in the time..

http://www.codeproject.com/dialog/WinMakeInactive.asp
 
Something like this with ColorMatrix, the form flickers some, and there are some setting to experiment with interval and timing, the code modifies the saturation of the image from 1.0 to 0.0.
VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button2.Click
  Me.SetStyle(ControlStyles.DoubleBuffer, True)
  Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
  Dim img As Image = Me.BackgroundImage.Clone
  For i As Single = 1 To 0 Step -0.05
    Me.BackgroundImage = saturation(img, i)
    Me.Refresh()
    Threading.Thread.Sleep(10)
  Next
End Sub
colormatrix/saturation code needed too:
VB.NET:
Public Function saturation(ByVal img As Image, ByVal s As Single) As Image
  Const lumR As Single = 0.3086
  Const lumG As Single = 0.6094
  Const lumB As Single = 0.082
  Dim a As Single = (1.0 - s) * lumR + s
  Dim b As Single = (1.0 - s) * lumR
  Dim c As Single = (1.0 - s) * lumR
  Dim d As Single = (1.0 - s) * lumG
  Dim e As Single = (1.0 - s) * lumG + s
  Dim f As Single = (1.0 - s) * lumG
  Dim g As Single = (1.0 - s) * lumB
  Dim h As Single = (1.0 - s) * lumB
  Dim i As Single = (1.0 - s) * lumB + s
  Dim cm As Drawing.Imaging.ColorMatrix = New Drawing.Imaging.ColorMatrix(New Single()() _
    {New Single() {a, b, c, 0, 0}, _
     New Single() {d, e, f, 0, 0}, _
     New Single() {g, h, i, 0, 0}, _
     New Single() {0, 0, 0, 1, 0}, _
     New Single() {0, 0, 0, 0, 1}})
  Return apply_cm(img, cm)
End Function
 
Public Function apply_cm(ByVal img As Image, ByVal cm As Drawing.Imaging.ColorMatrix) As Image
  'return new image with given ImageAttributes set
  Dim bm As Bitmap = New Bitmap(img.Width, img.Height)
  Dim g As Graphics = Graphics.FromImage(bm)
  Dim ia As New Drawing.Imaging.ImageAttributes
  ia.SetColorMatrix(cm)
  g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia)
  g.Dispose()
  Return bm
End Function
 
Back
Top