Results 1 to 4 of 4

Thread: How to do some pretty neat GDI+

Threaded View

  1. #1

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    How to do some pretty neat GDI+

    All the following code assumes you have imported System.Drawing and System.Drawing.Imaging.
    I'll also be creating a custom control, a class, or a set of extensions to Graphics, so you don't have to remember all of this.

    1. Using a ColorMatrix.

    A ColorMatrix is an object you (can) use to change the values of the R, G, B, and A values in an image by multiplication. It's always an array of arrays of singles, 5x5. A diagonal line of 1 values is the default. Here's how you can use it to fade out a picture:
    Code:
    Dim g As Graphics = Me.CreateGraphics() 'Use whatever graphics source, even a bitmap.
    Dim bmp As New Bitmap(Image.FromFile("example.png"))
    Dim alpha As Single = 0.5! ' Draw at half transparency
    Dim arr()() As Single = { _
         New Single() {1,0,0,0,0}, _
         New Single() {0,1,0,0,0}, _
         New Single() {0,0,1,0,0}, _
         New Single() {0,0,0,alpha,0}, _
         New Single() {0,0,0,0,1} _
    }
    Dim cm As New ColorMatrix(arr)
    Dim iattr As New ImageAttributes()
    iattr.SetColorMatrix(cm)
    g.DrawImage(bmp,New Rectangle(0,0,bmp.Width,bmp.Height),0,0,bmp.Width,bmp.Height,GraphicsUnit.Pixel,iattr)
    Just put that in a loop that increments or decrements "alpha".
    Next: rotation, scaling, skewing, and flipping - all in one!
    Last edited by minitech; Jun 5th, 2010 at 12:22 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width