dotnet-Snippets.com
Snippets: 61 | Registered User: 62 | Visitors online: 20
Main Menu

Home
Random Snippet
FAQs
Contact Us
Imprint
RSS Feeds

Rss All languages
Rss C#
Rss VB.NET
Rss C++
Rss J#
Rss ASP.NET
Google Ads

Sri Lanka .NET 
                Forum Member
Invert Colors from Image

Author: Khartak
Programming Language: VB.NET Rating:
not yet rated

Views: 2445

Description:

This function inverts the colors from an image by using a new color matrix



Visual Basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Public Function InvertColors(ByVal Image As Image) As Image
    Dim ImgAttr As New Imaging.ImageAttributes()

    'Default-ColorMatrix for Inverting
    Dim ColorMatrix As New Imaging.ColorMatrix(New 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, 1, 0}, New Single() {0, 0, 0, 0, 1}})

    'Apply ColorMatrix to Image
    ImgAttr.SetColorMatrix(ColorMatrix)

    'Create new 32bit Bitmap
    Dim NewBitmap = New Bitmap(Image.Width, Image.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)

    'Set resolution from Sourceimage to Targetimage
    NewBitmap.SetResolution(Image.HorizontalResolution, Image.VerticalResolution)

    'Create Graphicsobject from NewBitmap
    Dim NewGraphics As Graphics = Graphics.FromImage(NewBitmap)

    'Draw NewBitmap on NewGraphics
    NewGraphics.DrawImage(Image, New Rectangle(0, 0, NewBitmap.Width, NewBitmap.Height), 0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel, ImgAttr)

    'Dispose ressources
    NewGraphics.Dispose()
    ImgAttr.Dispose()
    Return NewBitmap
End Function


This Snippets could be interesting for you:

Poor Excellent
1 2 3 4 5 6 7 8 9 10
Sign in to vote for this snippet.

Comments:
(Please log in to wrtite an comment.)