CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2010
    Posts
    5

    Unhappy Need an expert advice regarding GDI & C#

    Ok, here is the example.

    Why XORCycleA doesn't work while XORCycleB works...

    Here is quite serious riddle for an expert.

    Hope you can "speak" C# and will understand my strings without additional comments )

    XOR_Paint will be invoked each time you move the dialog window and smth overlays it.


    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Drawing.Imaging;
    
    namespace ConsoleTestApplication
    {
     class Program
     {
     static void Main(string[] args)
     {
      new XOR().ShowDialog();
     }
     }
    
     internal class XOR : Form
     {
     [DllImport("gdi32.dll")]
     public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc,
                int nXSrc, int nYSrc, Int32 dwRop);
     [DllImport("gdi32.dll")]
     public static extern bool TextOut(IntPtr hdc, int x, int y, string lpString, int cbString);
    
     const Int32 SRCCOPY = 0x00CC0020;
     const Int32 SRCAND = 0x008800C6;
     const Int32 SRCINVERT = 0x00660046; // INVERT means XOR
    
     public XOR()
     {
      // Bit map created once with screen data in it
      bmA = new Bitmap(s.Width, s.Height, PixelFormat.Format32bppRgb);
      Graphics gfxBMP = Graphics.FromImage(bmA);
      gfxBMP.CopyFromScreen(0, 0, 0, 0, new Size(s.Width, s.Height), CopyPixelOperation.SourceCopy);
      gfxBMP.Dispose();
      this.Paint += new PaintEventHandler(XOR_Paint);
     }
    
     void XOR_Paint(object sender, PaintEventArgs e)
     {
      xorCycleA();
      xorCycleB();
     }
    
     Bitmap bmA;
     Size s = Screen.PrimaryScreen.Bounds.Size;
     unsafe void xorCycleA()
     {
      Graphics grfxScreen = Graphics.FromHwnd(IntPtr.Zero);
      Graphics grfxBitmap = Graphics.FromImage(bmA);
      bmA.Save(@"c:\before_xorA.bmp", ImageFormat.Bmp);
      IntPtr hdcBitmap = grfxBitmap.GetHdc();
      IntPtr hdcScreen = grfxScreen.GetHdc();
      // without any changes just copy screen data with XOR
      BitBlt(hdcBitmap, 0, 0, bmA.Width, bmA.Height, hdcScreen, 0, 0, SRCINVERT);
      grfxScreen.ReleaseHdc(hdcScreen);
      grfxBitmap.ReleaseHdc(hdcBitmap);
      grfxScreen.Dispose();
      grfxBitmap.Dispose();
      bmA.Save(@"c:\after_xorA.bmp", ImageFormat.Bmp);
     }
    
     Bitmap bmB;
     unsafe void xorCycleB()
     {
      Graphics grfxScreen = Graphics.FromHwnd(IntPtr.Zero);
      bmB = new Bitmap((int)grfxScreen.VisibleClipBounds.Width,
        (int)grfxScreen.VisibleClipBounds.Height, grfxScreen);
      Graphics grfxBitmap = Graphics.FromImage(bmB);
      bmB.Save(@"c:\before_xorB.bmp", ImageFormat.Bmp);
      IntPtr hdcBitmap = grfxBitmap.GetHdc();
      IntPtr hdcScreen = grfxScreen.GetHdc();
      BitBlt(hdcBitmap, 0, 0, bmB.Width, bmB.Height, hdcScreen, 0, 0, SRCCOPY);
      TextOut(hdcBitmap, 100, 100, "Hi, I'm here", 12);
      // comment BitBlt to make sure the bitmap contains screen data and the string on it inside after_xorB.bmp
      BitBlt(hdcBitmap, 0, 0, bmB.Width, bmB.Height, hdcScreen, 0, 0, SRCINVERT);
      grfxScreen.ReleaseHdc(hdcScreen);
      grfxBitmap.ReleaseHdc(hdcBitmap);
      grfxScreen.Dispose();
      grfxBitmap.Dispose();
      bmB.Save(@"c:\after_xorB.bmp", ImageFormat.Bmp);
     }
     }
    }
    Last edited by vitagoni; July 9th, 2011 at 12:26 PM.

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Need an expert advice regarding GDI & C#

    Hard to read a block of code wihtout proper formatting. Next time use code tags so the formatting is retained.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jan 2010
    Posts
    5

    Re: Need an expert advice regarding GDI & C#

    Quote Originally Posted by DataMiser View Post
    Hard to read a block of code wihtout proper formatting. Next time use code tags so the formatting is retained.
    Unfortunately,
    I can't find any formatting option during editing.
    That is why.. sorry.. my browser is Chrome.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Need an expert advice regarding GDI & C#

    You need to either use the enhanced interface available as an option Here near the bottom of the page.

    Or you can simply type them in [ code ] [ \code ] without the spaces inside the brackets.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Jan 2010
    Posts
    5

    Re: Need an expert advice regarding GDI & C#

    Quote Originally Posted by DataMiser View Post
    You need to either use the enhanced interface available as an option Here near the bottom of the page.

    Or you can simply type them in [ code ] [ \code ] without the spaces inside the brackets.
    thanks, done.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Need an expert advice regarding GDI & C#

    I'm far from an expert in C# and do very little with graphics but it appears there is no image in bmA
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Jan 2010
    Posts
    5

    Re: Need an expert advice regarding GDI & C#

    Quote Originally Posted by DataMiser View Post
    I'm far from an expert in C# and do very little with graphics but it appears there is no image in bmA
    no. the Bitmap contains screen shot.
    if you put Save after CopyFromScreen in Constructor you'd see screen shot.

Tags for this Thread

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

Featured