-
July 9th, 2011, 07:59 AM
#1
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|