Solved

paint in GDI to contrast background?

Posted on 2011-09-03
4
409 Views
Last Modified: 2012-05-12

Hello, if (using GDI) I need to annotate an arbitrary bitmap, I don't know what color is behind what I'm drawing.  It would be convenient if there was a brush or color that would automatically contrast the pixels behind it.  I remember this from way back in the day, BASICA had a XOR brush which would basically invert what was behind it.  Is it possible there is something BASICA could do that .NET can't?

Anyway is there any simple/fast technique in GDI to accomplish an automatic contrast (or distinction) with the background?  Obviously I could test the background, do some averaging, and choose a contrasting color but that's time consuming and exactly what im trying to avoid.
Also, alpha doesn't really work very well for this.

Any thoughts are appreciated, thanks.
0
Comment
Question by:riceman0
  • 2
4 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 36480495
I don't believe that .NET natively support XOR drawing, but you can always find a way to p/invoke, as shown in this example:

A .NET Implementation of the ZoomIn Utility
http://www.vbaccelerator.com/home/net/code/libraries/Graphics/ZoomIn/article.asp
0
 
LVL 15

Expert Comment

by:x77
ID: 36480683
I use ControlPaint.DrawReversibleFrame to allow user select a an area over my control.

The first time I draw a ReversibleFrame, it shows the a visible rectangle.
When I draw same rectangle, it restore the original, then I can draw a new rectangle as user moved the mouse.

Note that this method uses Screen coordinates not control coordinates.

Note that ControlPaint includes also DrawReversibleLine.
0
 
LVL 15

Assisted Solution

by:x77
x77 earned 250 total points
ID: 36480852
Gdi has support to many modes when paint. (COPYPEN, MASKPEN, MERGENOTPEN ...)
You can see SetRop2 Api: http://msdn.microsoft.com/en-us/library/dd145088(v=vs.85).aspx

A Sample with SetRop2: http://www.answers.com/topic/setrop2

Note: DrawReversibleRectangle uses RopMode: R2_XORPEN (7) or R2NotXorPen (10) depending on BackColor.
Here code from this method with reflector.

     
<UIPermission(SecurityAction.LinkDemand, Window:=UIPermissionWindow.AllWindows)> _
Public Shared Sub DrawReversibleLine(ByVal start As Point, ByVal [end] As Point, ByVal backColor As Color)
    Dim nDrawMode As Integer = ControlPaint.GetColorRop(backColor, 10, 7)
    Dim handle As IntPtr = UnsafeNativeMethods.GetDCEx(New HandleRef(Nothing, UnsafeNativeMethods.GetDesktopWindow), NativeMethods.NullHandleRef, &H403)
    Dim ptr2 As IntPtr = SafeNativeMethods.CreatePen(0, 1, ColorTranslator.ToWin32(backColor))
    Dim num2 As Integer = SafeNativeMethods.SetROP2(New HandleRef(Nothing, handle), nDrawMode)
    Dim ptr3 As IntPtr = SafeNativeMethods.SelectObject(New HandleRef(Nothing, handle), New HandleRef(Nothing, UnsafeNativeMethods.GetStockObject(5)))
    Dim ptr4 As IntPtr = SafeNativeMethods.SelectObject(New HandleRef(Nothing, handle), New HandleRef(Nothing, ptr2))
    SafeNativeMethods.MoveToEx(New HandleRef(Nothing, handle), start.X, start.Y, Nothing)
    SafeNativeMethods.LineTo(New HandleRef(Nothing, handle), [end].X, [end].Y)
    SafeNativeMethods.SetROP2(New HandleRef(Nothing, handle), num2)
    SafeNativeMethods.SelectObject(New HandleRef(Nothing, handle), New HandleRef(Nothing, ptr3))
    SafeNativeMethods.SelectObject(New HandleRef(Nothing, handle), New HandleRef(Nothing, ptr4))
    SafeNativeMethods.DeleteObject(New HandleRef(Nothing, ptr2))
    UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, New HandleRef(Nothing, handle))
End Sub

Open in new window


To Get screen coordinates, use  Control.PointToScreen where control is your control.


'If DragMode, When Left Mouse Down, Start Drag Operation
    Private Sub PrvCtl_MouseDown (ByVal sender As Object, ByVal e As MouseEventArgs) Handles PrvCtl.MouseDown
        If DragMode Then
            DragMode = False
            rcold = New Rectangle (PrvCtl.PointToScreen (New Point (0, e.Y)), New Size (PrvCtl.Width, 0))
            PrvCtl.AllowDrop = True
            PrvCtl.DoDragDrop (New DataObject ("e", e), DragDropEffects.Move)
        End If
    End Sub

Open in new window

0
 

Author Closing Comment

by:riceman0
ID: 36511616
Thanks, both look promising.  I will post back with some sort of solution/resolution.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Service to Receive TCP Packets 4 43
Create XML 5 34
Format column on datatable 7 17
C# TextBox 11 13
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now