VB 5/6-Tipp 0680: Intensität der Farbkanäle CMYK mit GDI+ darstellen
von Frank Schüler
Beschreibung
Dieses Beispiel zeigt, wie mittels GDI+ die Intensität der einzelnen Farbkanäle CMYK in Graustufen angezeigt werden kann.
Schwierigkeitsgrad: | Verwendete API-Aufrufe: GdipCreateBitmapFromGraphics, GdipCreateFromHDC, GdipCreateImageAttributes, GdipDeleteGraphics, GdipDisposeImage, GdipDisposeImageAttributes, GdipDrawImageRect, GdipDrawImageRectRect, GdipGetImageDimension, GdipGetImageGraphicsContext, GdipLoadImageFromFile, GdipSetImageAttributesOutputChannel, GdiplusShutdown, GdiplusStartup | Download: |
'Dieser Quellcode stammt von http://www.activevb.de 'und kann frei verwendet werden. Für eventuelle Schäden 'wird nicht gehaftet. 'Um Fehler oder Fragen zu klären, nutzen Sie bitte unser Forum. 'Ansonsten viel Spaß und Erfolg mit diesem Source! '------- Anfang Projektdatei GDIPlusOutputChannel.vbp ------- ' Die Komponente 'Microsoft Common Dialog Control 6.0 (SP3) (comdlg32.ocx)' wird benötigt. '--- Anfang Formular "frmGDIPlusOutputChannel" alias frmGDIPlusOutputChannel.frm --- ' Steuerelement: Bildfeld-Steuerelement "picChannel" (Index von 0 bis 0) ' Steuerelement: Schaltfläche "cmdLoadPicture" ' Steuerelement: Standarddialog-Steuerelement "CommonDialog1" Option Explicit ' ----==== GDI+ Konstenten ====---- Private Const GdiPlusVersion As Long = 1& ' ----==== GDI+ Typen ====---- Private Type GDIPlusStartupInput GdiPlusVersion As Long DebugEventCallback As Long SuppressBackgroundThread As Long SuppressExternalCodecs As Long End Type Private Type GdiplusStartupOutput NotificationHook As Long NotificationUnhook As Long End Type ' ----==== GDI+ Enumerationen ====---- Private Enum ColorAdjustType ColorAdjustTypeDefault = 0 ColorAdjustTypeBitmap = 1 ColorAdjustTypeBrush = 2 ColorAdjustTypePen = 3 ColorAdjustTypeText = 4 ColorAdjustTypeCount = 5 ColorAdjustTypeAny = 6 End Enum Private Enum ColorChannelFlags ColorChannelFlagsC = 0 ColorChannelFlagsM = 1 ColorChannelFlagsY = 2 ColorChannelFlagsK = 3 ColorChannelFlagsLast = 4 End Enum 'GDI+ Status Private Enum Status OK = 0 GenericError = 1 InvalidParameter = 2 OutOfMemory = 3 ObjectBusy = 4 InsufficientBuffer = 5 NotImplemented = 6 Win32Error = 7 WrongState = 8 Aborted = 9 FileNotFound = 10 ValueOverflow = 11 AccessDenied = 12 UnknownImageFormat = 13 FontFamilyNotFound = 14 FontStyleNotFound = 15 NotTrueTypeFont = 16 UnsupportedGdiplusVersion = 17 GdiplusNotInitialized = 18 PropertyNotFound = 19 PropertyNotSupported = 20 ProfileNotFound = 21 End Enum Private Enum Unit UnitWorld = 0 UnitDisplay = 1 UnitPixel = 2 UnitPoint = 3 UnitInch = 4 UnitDocument = 5 UnitMillimeter = 6 End Enum ' ----==== GDI+ API Deklarationen ====---- Private Declare Function GdipCreateBitmapFromGraphics Lib "gdiplus" _ (ByVal Width As Long, ByVal Height As Long, _ ByVal target As Long, ByRef bitmap As Long) As Status Private Declare Function GdipCreateFromHDC Lib "gdiplus" _ (ByVal hdc As Long, ByRef graphics As Long) As Status Private Declare Function GdipCreateImageAttributes Lib "gdiplus" _ (ByRef imageattr As Long) As Status Private Declare Function GdipDeleteGraphics Lib "gdiplus" _ (ByVal graphics As Long) As Status Private Declare Function GdipDisposeImage Lib "gdiplus" _ (ByVal image As Long) As Status Private Declare Function GdipDisposeImageAttributes Lib "gdiplus" _ (ByVal imageattr As Long) As Status Private Declare Function GdipDrawImageRect Lib "gdiplus" _ (ByVal graphics As Long, ByVal image As Long, _ ByVal X As Single, ByVal Y As Single, _ ByVal Width As Single, ByVal Height As Single) As Status Private Declare Function GdipDrawImageRectRect Lib "gdiplus" _ (ByVal graphics As Long, ByVal image As Long, _ ByVal dstx As Single, ByVal dsty As Single, _ ByVal dstwidth As Single, ByVal dstheight As Single, _ ByVal srcx As Single, ByVal srcy As Single, _ ByVal srcwidth As Single, ByVal srcheight As Single, _ ByVal srcUnit As Unit, _ Optional ByVal imageAttributes As Long = 0, _ Optional ByVal callback As Long = 0, _ Optional ByVal callbackData As Long = 0) As Status Private Declare Function GdipGetImageDimension Lib "gdiplus" _ (ByVal image As Long, ByRef Width As Single, _ ByRef Height As Single) As Status Private Declare Function GdipGetImageGraphicsContext Lib "gdiplus" _ (ByVal image As Long, ByRef graphics As Long) As Status Private Declare Function GdipSetImageAttributesOutputChannel _ Lib "gdiplus" (ByVal imageattr As Long, _ ByVal ColorAdjust As ColorAdjustType, _ ByVal enableFlag As Boolean, _ ByVal channelFlags As ColorChannelFlags) As Status Private Declare Function GdipLoadImageFromFile Lib "gdiplus" _ (ByVal FileName As Long, ByRef image As Long) As Status Private Declare Function GdiplusShutdown Lib "gdiplus" _ (ByVal token As Long) As Status Private Declare Function GdiplusStartup Lib "gdiplus" _ (ByRef token As Long, ByRef lpInput As GDIPlusStartupInput, _ ByRef lpOutput As GdiplusStartupOutput) As Status ' ----==== Variablen ====---- Dim GdipToken As Long Dim GdipInitialized As Boolean '------------------------------------------------------ ' Funktion : DrawImageChannelFromFile ' Beschreibung : Lädt ein Bilddatei per GDI+ vom Datenträger ' und zeigt die Intensität des ' angegeben Farbkanales an ' Übergabewert : sFileName = Pfad\Dateiname der Bilddatei ' lDrawHdc = HDC vom Objekt in dem gezeichnet ' werden soll ' eChannelFlag = Enum ColorChannelFlags '------------------------------------------------------ Private Sub DrawImageChannelFromFile(ByVal sFileName As String, _ ByVal lDrawHdc As Long, ByVal eChannelFlag As ColorChannelFlags) Dim lBitmap As Long Dim lBitmap2 As Long Dim lGraphics As Long Dim lGraphics2 As Long Dim lImgAttr As Long Dim sImageWidth As Single Dim sImageHeight As Single ' Erzeugt ein Graphicsobjekt ' vom Hdc -> lGraphics If Execute(GdipCreateFromHDC(lDrawHdc, _ lGraphics)) = OK Then ' Laden der Bilddatei und erzeugen eines ' Bitmapobjekts sFileName -> lBitmap If Execute(GdipLoadImageFromFile(StrPtr(sFileName), _ lBitmap)) = OK Then ' Ermitteln der Dimensionen des Bitmapobjekts Call Execute(GdipGetImageDimension(lBitmap, _ sImageWidth, sImageHeight)) ' Erzeugt ein ImageAttributesobjekt If Execute(GdipCreateImageAttributes( _ lImgAttr)) = OK Then ' Setzen des OutputChannel für das ' ImageAttributesobjekt If Execute(GdipSetImageAttributesOutputChannel( _ lImgAttr, ColorAdjustTypeDefault, _ True, eChannelFlag)) = OK Then ' Erzeugt ein Graphicsobjekt vom ' Bitmapobjekt lBitmap -> lGraphics2 If Execute(GdipGetImageGraphicsContext( _ lBitmap, lGraphics2)) = OK Then ' Erzeugt ein Bitmapobjekt vom ' Graphicsobjekt lGraphics2 -> lBitmap2 If Execute(GdipCreateBitmapFromGraphics( _ sImageWidth, sImageHeight, lGraphics2, _ lBitmap2)) = OK Then ' Löschen von lGraphics2 Call Execute(GdipDeleteGraphics( _ lGraphics2)) ' Erzeugt ein Graphicsobjekt vom ' Bitmapobjekt ' lBitmap2 -> lGraphics2 If Execute(GdipGetImageGraphicsContext( _ lBitmap2, lGraphics2)) = OK Then ' Zeichnet lBitmap in das ' Graphicsobjekt lGraphics2 mit den ' entsprechenden ImageAttributen Call Execute(GdipDrawImageRectRect( _ lGraphics2, lBitmap, _ 0, 0, sImageWidth, sImageHeight, _ 0, 0, sImageWidth, sImageHeight, _ UnitPixel, lImgAttr)) ' Löscht lGraphics2 Call Execute(GdipDeleteGraphics( _ lGraphics2)) ' Löscht lBitmap Call Execute(GdipDisposeImage( _ lBitmap)) ' Zeichnet lBitmap2 in das ' Graphicsobjektes lGraphics2 Call Execute(GdipDrawImageRect( _ lGraphics, lBitmap2, 0, 0, _ sImageWidth, sImageHeight)) ' Löscht lBitmap2 Call Execute(GdipDisposeImage( _ lBitmap2)) End If End If End If End If ' Löscht lImgAttr Call Execute(GdipDisposeImageAttributes(lImgAttr)) End If End If ' Löscht lGraphics Call Execute(GdipDeleteGraphics(lGraphics)) End If End Sub '------------------------------------------------------ ' Funktion : DrawImageFromFile ' Beschreibung : Lädt ein Bilddatei per GDI+ vom Datenträger ' Übergabewert : sFileName = Pfad\Dateiname der Bilddatei ' lDrawHdc = HDC vom Objekt in dem ' gezeichnet werden soll '------------------------------------------------------ Private Sub DrawImageFromFile(ByVal sFileName As String, _ ByVal lDrawHdc As Long) Dim lBitmap As Long Dim lGraphics As Long Dim sImageWidth As Single Dim sImageHeight As Single ' Erzeugen eines Graphicsobjektes ' vom Hdc -> lGraphics If Execute(GdipCreateFromHDC(lDrawHdc, _ lGraphics)) = OK Then ' Laden der Bilddatei -> lBitmap If Execute(GdipLoadImageFromFile(StrPtr(sFileName), _ lBitmap)) = OK Then ' Ermitteln der Dimensionen von lBitmap Call Execute(GdipGetImageDimension(lBitmap, _ sImageWidth, sImageHeight)) ' Zeichnet lBitmap in lGraphics Call Execute(GdipDrawImageRect(lGraphics, _ lBitmap, 0, 0, sImageWidth, sImageHeight)) ' Löscht lBitmap Call Execute(GdipDisposeImage(lBitmap)) End If ' Löscht lGraphics Call Execute(GdipDeleteGraphics(lGraphics)) End If End Sub '------------------------------------------------------ ' Funktion : Execute ' Beschreibung : Gibt im Fehlerfall die entsprechende ' GDI+ Fehlermeldung aus ' Übergabewert : GDI+ Status ' Rückgabewert : GDI+ Status '------------------------------------------------------ Private Function Execute(ByVal lReturn As Status) As Status Dim lCurErr As Status If lReturn = OK Then lCurErr = OK Else lCurErr = lReturn MsgBox GdiErrorString(lReturn) & " GDI+ Error:" _ & lReturn, vbOKOnly, "GDI Error" End If Execute = lCurErr End Function '------------------------------------------------------ ' Funktion : GdiErrorString ' Beschreibung : Umwandlung der GDI+ Statuscodes in Stringcodes ' Übergabewert : GDI+ Status ' Rückgabewert : Fehlercode als String '------------------------------------------------------ Private Function GdiErrorString(ByVal lError As Status) As String Dim s As String Select Case lError Case GenericError: s = "Generic Error." Case InvalidParameter: s = "Invalid Parameter." Case OutOfMemory: s = "Out Of Memory." Case ObjectBusy: s = "Object Busy." Case InsufficientBuffer: s = "Insufficient Buffer." Case NotImplemented: s = "Not Implemented." Case Win32Error: s = "Win32 Error." Case WrongState: s = "Wrong State." Case Aborted: s = "Aborted." Case FileNotFound: s = "File Not Found." Case ValueOverflow: s = "Value Overflow." Case AccessDenied: s = "Access Denied." Case UnknownImageFormat: s = "Unknown Image Format." Case FontFamilyNotFound: s = "FontFamily Not Found." Case FontStyleNotFound: s = "FontStyle Not Found." Case NotTrueTypeFont: s = "Not TrueType Font." Case UnsupportedGdiplusVersion: s = "Unsupported Gdiplus Version." Case GdiplusNotInitialized: s = "Gdiplus Not Initialized." Case PropertyNotFound: s = "Property Not Found." Case PropertyNotSupported: s = "Property Not Supported." Case Else: s = "Unknown GDI+ Error." End Select GdiErrorString = s End Function '------------------------------------------------------ ' Funktion : StartUpGDIPlus ' Beschreibung : Initialisiert GDI+ Instanz ' Übergabewert : GDI+ Version ' Rückgabewert : GDI+ Status '------------------------------------------------------ Private Function StartUpGDIPlus(ByVal GdipVersion As Long) As Status ' Initialisieren der GDI+ Instanz Dim GdipStartupInput As GDIPlusStartupInput Dim GdipStartupOutput As GdiplusStartupOutput GdipStartupInput.GdiPlusVersion = GdipVersion StartUpGDIPlus = GdiplusStartup(GdipToken, _ GdipStartupInput, GdipStartupOutput) End Function '------------------------------------------------------ ' Funktion : ShutdownGDIPlus ' Beschreibung : Beendet die GDI+ Instanz ' Rückgabewert : GDI+ Status '------------------------------------------------------ Private Function ShutdownGDIPlus() As Status ' Beendet GDI+ Instanz ShutdownGDIPlus = GdiplusShutdown(GdipToken) End Function Private Sub cmdLoadPicture_Click() Dim z As Long ' Fehlerbehandlung On Error Goto errorhandler ' ist GDI+ initialisiert If GdipInitialized = True Then ' Dialogparameter setzen With CommonDialog1 .Filter = _ "Images Files (*.bmp;*.gif;*.jpg;*.png;*.tif)" _ & "|*.bmp;*.gif;*.jpg;*.png;*.tif" .CancelError = True .ShowOpen End With ' Inhalt in PictureBoxen löschen For z = 0 To 4 picChannel(z).Cls Next z ' Zeichnet das Original Bild Call DrawImageFromFile(CommonDialog1.FileName, _ picChannel(0).hdc) ' Zeichnet das Original Bild und zeigt die ' Intensität vom Farbkanal Cyan in Graustufen Call DrawImageChannelFromFile(CommonDialog1.FileName, _ picChannel(1).hdc, ColorChannelFlagsC) ' Zeichnet das Original Bild und zeigt die ' Intensität vom Farbkanal Magenta in Graustufen Call DrawImageChannelFromFile(CommonDialog1.FileName, _ picChannel(2).hdc, ColorChannelFlagsM) ' Zeichnet das Original Bild und zeigt die ' Intensität vom Farbkanal Yellow in Graustufen Call DrawImageChannelFromFile(CommonDialog1.FileName, _ picChannel(3).hdc, ColorChannelFlagsY) ' Zeichnet das Original Bild und zeigt die ' Intensität vom Farbkanal Black in Graustufen Call DrawImageChannelFromFile(CommonDialog1.FileName, _ picChannel(4).hdc, ColorChannelFlagsK) ' Refresh der PictureBoxen For z = 0 To 4 picChannel(z).Refresh Next z End If Exit Sub errorhandler: End Sub Private Sub Form_Load() Dim z As Long GdipInitialized = False ' Form Parameter setzen With Me .ScaleMode = vbTwips .Height = 7020 .Width = 9360 End With ' Button Parameter setzen With cmdLoadPicture .Move 60, 60, 1700, 375 .Caption = "Load Picture" End With ' PicturBox Parameter setzen With picChannel(0) .Move 60, 60 + cmdLoadPicture.Top _ + cmdLoadPicture.Height, 3000, 3000 .AutoRedraw = True End With ' weitere PicturBoxen laden For z = 1 To 4 Load picChannel(z) picChannel(z).Visible = True Next z ' neue PictureBoxen positionieren picChannel(1).Move 60 + picChannel(0).Left _ + picChannel(0).Width, picChannel(0).Top picChannel(2).Move 60 + picChannel(1).Left _ + picChannel(1).Width, picChannel(0).Top picChannel(3).Move picChannel(1).Left, _ 60 + picChannel(1).Top + picChannel(1).Height picChannel(4).Move picChannel(2).Left, _ 60 + picChannel(2).Top + picChannel(2).Height ' Initialisieren von GDI+ If Execute(StartUpGDIPlus(GdiPlusVersion)) = OK Then GdipInitialized = True Else ' Initialisierung fehlgeschlagen MsgBox "GDI+ not inizialized.", vbOKOnly, "GDI Error" End If End Sub Private Sub Form_Unload(Cancel As Integer) ' ist GDI+ initialisiert If GdipInitialized = True Then ' GDI+ beenden Call Execute(ShutdownGDIPlus) End If End Sub '--- Ende Formular "frmGDIPlusOutputChannel" alias frmGDIPlusOutputChannel.frm --- '-------- Ende Projektdatei GDIPlusOutputChannel.vbp --------
Tipp-Kompatibilität:
Windows/VB-Version | Win32s | Win95 | Win98 | WinME | WinNT4 | Win2000 | WinXP |
VB4 | |||||||
VB5 | |||||||
VB6 |
Ihre Meinung
Falls Sie Fragen zu diesem Artikel haben oder Ihre Erfahrung mit anderen Nutzern austauschen möchten, dann teilen Sie uns diese bitte in einem der unten vorhandenen Themen oder über einen neuen Beitrag mit. Hierzu können sie einfach einen Beitrag in einem zum Thema passenden Forum anlegen, welcher automatisch mit dieser Seite verknüpft wird.