# Forum > Diablo 3 > Diablo 3 Bots and Programs >  [Source] How to BOT in C#

## Shoruun

hi, 

I saw several thread with autoit bots. And many people who choose autoit, because they had problems with c#.
So, i will show you some of my core source, which will allow you to make bots in C#.

First of all we need to know gamestates. This is done by pixel colors, to be patch safe. Due to Directx problems, you have to use WINDOW MODE in Diablo 3 to succesfully get the pixel color.
Just find your a pixel, which is always the same color at a gamestate. for example the background at login screen, the button at loginloading screen, or the monsters near the healthball ingame.
To read the pixel and check it just use following code:



```
static public Color GetColorAt(Point location)
        {
            
            using (Graphics gdest = Graphics.FromImage(screenPixel))
            {
                using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
                {
                    IntPtr hSrcDC = gsrc.GetHdc();
                    IntPtr hDC = gdest.GetHdc();
                    int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);
                    gdest.ReleaseHdc();
                    gsrc.ReleaseHdc();
                }
            }

            return screenPixel.GetPixel(0, 0);
        }
```

To get the right coords in the window, and always if you move d3 window, use a convert method to get right pixel coords:



```
[DllImport("user32.dll")]
        private static extern int GetWindowRect(IntPtr hwnd, out Rectangle rect);
        static private Point ConvertToScreenPixel(Point point)
        {
            Rectangle rect;

            GetWindowRect(D3MemReader.getInstance().getD3WinHandle(), out rect);

            Point ret = new Point();

            ret.X = rect.Location.X + point.X;
            ret.Y = rect.Location.Y + point.Y;

            return ret;
        }
```

And here is a sample check method of my bot:


```
static public bool isInGame()
        {
            Point pixel = ConvertToScreenPixel(new Point(125, 598));
            
            Color c = GetColorAt(pixel);
 
            return (c.Name == "ff5d574c");
        }
```

The next important point is how to make a click in d3? IMPORTANT thing here is the waittime, it reduces the chance of a ban =)


```
[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
static public int LeftClick(int x, int y, bool random_sleep = true, int lowest_sleep = 100, int highes_sleep = 200, bool chest_click = false)
        {
            int sleep = 0;
            if (random_sleep)
            {
                sleep = random.Next(lowest_sleep, highes_sleep);
                System.Threading.Thread.Sleep(sleep);
            }

            Point tmp = Cursor.Position;

            mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
            Cursor.Position = ConvertToScreenPixel(new Point(x, y));
            if(chest_click)
                System.Threading.Thread.Sleep(50);
            mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
            mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);


            Cursor.Position = tmp;

            return sleep;
        }
```

MAKE SURE YOU ALWAYS CALL THIS METHOD WITH RANDOM COORDS!


```
Tools.LeftClick(rnd.Next(278, 285), rnd.Next(230, 235), true, big_run_time_min, big_run_time_max, true);
```

Next important thing would be to press a key. Currently i only use this for reconnect, because u are able to do all other things with the mouse.


```
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
static public int PressKey(Keys key, bool random_sleep = true, int lowest_sleep = 100, int highes_sleep = 200)
        {
            int sleep = 0;
            if (random_sleep)
            {
                sleep = random.Next(lowest_sleep, highes_sleep);
                System.Threading.Thread.Sleep(sleep);
            }

            const int KEYEVENTF_EXTENDEDKEY = 0x1;
            const int KEYEVENTF_KEYUP = 0x2;

            keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
            System.Threading.Thread.Sleep(random.Next(5, 10));
            keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);

            return sleep;
        }
```

With this pieces of code your are already able to make a nice bot.
Hope it helps u a bit =)

-Shoruun

----------


## hovsa

nice, as a Webdeveloper myself i was using AutoIT because i didnt have the first clue on how to do these things in WinForms and not having access to all the luxuries of C# is driving me crazy, i just might do my first ever WinForms project now  :Smile:

----------


## Shoruun

you r welcome =)

----------


## CreativeXtent

Very nice +! rep

----------


## Sychotix

Eww... programming a bot with pixel detection. Regardless, I'm sure people will find this to be useful. +Rep.

----------


## Arakkoa

> Eww... programming a bot with pixel detection. Regardless, I'm sure people will find this to be useful. +Rep.


Well, screen scraping reduces the chances of getting banned as there is no need to inject code, or read memory or anything of the sorts.

----------


## kyllesdk

GetWindowRect(D3MemReader.getInstance().getD3WinHandle(), out rect);

I det the error that D3MemReader dosen't exist in the current context.

What is the D3MemReader? Some code that you didn't include in this post?

----------


## who knows

> nice, as a Webdeveloper myself i was using AutoIT because i didnt have the first clue on how to do these things in WinForms and not having access to all the luxuries of C# is driving me crazy, i just might do my first ever WinForms project now


I've written bots in Visual Basic of all things, why wouldn't c# in visual studio work? Haven't used the language myself yet, I've just heard that they're similar, C++ & VB combined = C#.

----------


## hecccy

i wish i knew how to code properly, C# is one of the only things i know but this is still too complex for me

----------


## jockep

> GetWindowRect(D3MemReader.getInstance().getD3WinHandle(), out rect);
> 
> I det the error that D3MemReader dosen't exist in the current context.
> 
> What is the D3MemReader? Some code that you didn't include in this post?


It's using some custom function to read the handle of the Diablo 3 window. You could use this


```
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

IntPtr hWnd = (IntPtr)FindWindow("Diablo III", null);
```

and replace D3MemReader.getInstance().getD3WinHandle() with hWnd

Didn't try it tho.

----------


## Shoruun

and the same without DllImport:


```
public IntPtr getD3WinHandle()
{
	Process[] processes = Process.GetProcessesByName("Diablo III");
	if (processes.Count() > 1)
		throw new Exception("Too many Processes named Diablo 3!");

	if (processes.Count() == 0)
		throw new Exception("Diablo 3 not found!");

	return processes[0].MainWindowHandle;
}
```

i am saving this in a singleton, so i only need to get it once.

----------


## muskatnuss

Wow this is very usefull! Thanks for that pieces of code!

I have only written one app in c# it was a simple maths program so far. well i know the basics here but i really do got stuck with this "BitBlt" i really dont get the point with this one ?!
What is it and where do iget it? 
I have read about it everything i was able to find.

Maybe you Shoruun can help me out or anybody else who knows c# and its librarys 

Cheers

----------


## DozyJr

> Wow this is very usefull! Thanks for that pieces of code!
> 
> I have only written one app in c# it was a simple maths program so far. well i know the basics here but i really do got stuck with this "BitBlt" i really dont get the point with this one ?!
> What is it and where do iget it? 
> I have read about it everything i was able to find.
> 
> Maybe you Shoruun can help me out or anybody else who knows c# and its librarys 
> 
> Cheers


found this link: BitBlt : Windows API*«*Windows*«*C# / C Sharp

Just put this in your code:


```
[DllImportAttribute("gdi32.dll")]
        private static extern int BitBlt
        (
            IntPtr hdcDest,     // handle to destination DC (device context)
            int nXDest,         // x-coord of destination upper-left corner
            int nYDest,         // y-coord of destination upper-left corner
            int nWidth,         // width of destination rectangle
            int nHeight,        // height of destination rectangle
            IntPtr hdcSrc,      // handle to source DC
            int nXSrc,          // x-coordinate of source upper-left corner
            int nYSrc,          // y-coordinate of source upper-left corner
            System.Int32 dwRop  // raster operation code
        );
```

Thanks for this topic - really useful  :Smile: 

cheers

----------


## egangster

hmmm, hello  :Smile: 

This may sound a bit lame question but...how do you fill in the screenPixel variable? I'm guessing you should have a method to get a snapshot of the game window and pass it to the screenPixel variable as an Image ?

----------

