-
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.
-
July 9th, 2011, 10:06 AM
#2
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.
-
July 9th, 2011, 10:37 AM
#3
Re: Need an expert advice regarding GDI & C#
Originally Posted by DataMiser
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.
-
July 9th, 2011, 11:20 AM
#4
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.
-
July 9th, 2011, 12:26 PM
#5
Re: Need an expert advice regarding GDI & C#
Originally Posted by DataMiser
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.
-
July 9th, 2011, 12:38 PM
#6
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.
-
July 9th, 2011, 02:41 PM
#7
Re: Need an expert advice regarding GDI & C#
Originally Posted by DataMiser
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|