Index out of bounds when calculating pixel index - c#

I have my image cropping app based on:
http://www.c-sharpcorner.com/UploadFile/55275a/windowsphone-image-crop-with-rectangle/
I modified it a little bit so I can resize rectangle instead of creating new
so my whole method look like:
private async void Accept_Click(object sender, EventArgs e)
{
WriteableBitmap wb = new WriteableBitmap(image);
double originalImageWidth = wb.PixelWidth;
double originalImageHeight = wb.PixelHeight;
double displayedWidth = image1.ActualWidth;
double displayedHeight = image1.ActualHeight;
double widthRatio = originalImageWidth / displayedWidth;
double heightRatio = originalImageHeight / displayedHeight;
r = (Rectangle) (from c in LayoutRoot.Children
where c.Opacity == .5 select c).First();
GeneralTransform gt = r.TransformToVisual(LayoutRoot);
Point p = gt.Transform(new Point(0, 0));
Point1 = (r.TransformToVisual(this)).Transform(new Point(0, 0));
Point2 = new Point(Point1.X + r.Width, Point1.Y + r.Height);
WriteableBitmap newImage = new WriteableBitmap(
(int) (widthRatio * Math.Abs(Point2.X - Point1.X)),
(int) (heightRatio * Math.Abs(Point2.Y - Point1.Y)));
int xoffset = (int) (((Point1.X < Point2.X) ? Point1.X : Point2.X) * widthRatio);
int yoffset = (int) (((Point1.Y < Point2.Y) ? Point1.Y : Point2.X) * heightRatio);
if (newImage.Pixels.Length > 0)
{
for (int i = 0; i < newImage.Pixels.Length; i++)
{
int x = (int) ((i % newImage.PixelWidth) + xoffset);
int y = (int) ((i / newImage.PixelWidth) + yoffset);
newImage.Pixels[i] = wb.Pixels[y * wb.PixelWidth + x];
}
using (MemoryStream ms = new MemoryStream())
{
newImage.SaveJpeg(ms, (int) newImage.PixelWidth,
(int) newImage.PixelHeight, 0, 100);
image.SetSource(ms);
}
}
else
{
}
ProgressBar pb = new ProgressBar();
pb.IsEnabled = true;
LayoutRoot.Children.Add(pb);
int idReceipt = (int) PhoneApplicationService.Current.State["paragon"];
await ReceiptsHelper.addPhotosToReceipt(image, idReceipt);
NavigationService.Navigate(new Uri("/7.0/StronaParagonu.xaml", UriKind.Relative));
}
When my image is vertical everything works fine, but when my image is horizontal I get Array Index Out Of Bounds Exception at:
newImage.Pixels[i] = wb.Pixels[y * wb.PixelWidth + x];
I don't know exactly what am I doing wrong.
Anybody can help?
EDIT:
I changed my code to:
if (wb.PixelWidth > wb.PixelHeight)
{
for (int i = 0; i < newImage.Pixels.Length; i++)
{
int x = (int)((i % newImage.PixelWidth) + xoffset);
int y = (int)((i / newImage.PixelWidth) + yoffset);
newImage.Pixels[i] = wb.Pixels[x * wb.PixelHeight + y];
}
}
else
{
for (int i = 0; i < newImage.Pixels.Length; i++)
{
int x = (int)((i % newImage.PixelWidth) + xoffset);
int y = (int)((i / newImage.PixelWidth) + yoffset);
newImage.Pixels[i] = wb.Pixels[y * wb.PixelWidth + x];
}
}
In result I've got something like this
Maybe there is some workaround in which I can flip image 90 degree?
I tried WriteableBitmapEx but it doesnt work.

First thing I see is:
int yoffset = (int) (((Point1.Y < Point2.Y) ? Point1.Y : Point2.X) * heightRatio);
I guess there should be Point2.Y?
Second thing is what #Rashed mentioned in comment:
When you want get horizontal image, you must change X and Y place: newImage.Pixels[i] = wb.Pixels[x * wb.PixelHeight + y];
But please take a note I'm really guessing here since it's late and I don't see sharp ;-)

Related

A generic error occurred in GDI+ During a Bitmap Save

When I run this code
Bitmap im = new Bitmap(600, 600, PixelFormat.Format16bppGrayScale);
int height = im.Height;
int width = im.Width;
Point p1 = new Point(0, 0);
Point p2 = new Point(im.Size.Width, 0);
Point p3 = new Point(im.Width / 2, im.Height);
Random r = new Random();
Point p = new Point(r.Next(0, im.Size.Width), r.Next(0, im.Size.Height));
BitmapData data = im.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format16bppGrayScale);
const int stride = 2;
int wsrtide = data.Stride;
unsafe
{
IntPtr osc0 = data.Scan0;
ushort* sc0 = (ushort*)data.Scan0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
*sc0 = 0;
sc0 += 1;
}
}
for (long i = 0; i < width * height; i++)
{
if (i % 1_000_000 == 0)
{
Console.WriteLine(i);
}
var ran = r.Next(0, 3);
Point tp;
switch (ran)
{
case 0:
tp = new Point((p1.X + p.X) / 2, (p1.Y + p.Y) / 2);
sc0 = (ushort*)((int)osc0 + (wsrtide * tp.Y + (tp.X * stride)));
*sc0 = ushort.MaxValue;
p = tp;
break;
case 1:
tp = new Point((p2.X + p.X) / 2, (p2.Y + p.Y) / 2);
sc0 = (ushort*)((int)osc0 + (wsrtide * tp.Y + (tp.X * stride)));
*sc0 = ushort.MaxValue;
p = tp;
break;
case 2:
tp = new Point((p3.X + p.X) / 2, (p3.Y + p.Y) / 2);
sc0 = (ushort*)((int)osc0 + (wsrtide * tp.Y + (tp.X * stride)));
*sc0 = ushort.MaxValue;
p = tp;
break;
}
}
im.UnlockBits(data);
im.Save(Environment.CurrentDirectory + "\\img.png");
im.Dispose();
It throws where I save the bitmap
System.Runtime.InteropServices.ExternalException: A generic error
occurred in GDI+.`
I am almost certain that this isn't caused by file permissions, when I give the program admin permission and delete the image to reset permissions it still throws. I can confirm that this code swapped with BitmapSetPixel() on RGB works.
My suspicion is that I'm messing up the pointers, but I'm not really sure. Also, curiously enough, it makes empty png files even though it throws.
The purpose of this code is to generate a Sierpinski triangle using the chaos game method.
Your problem is the Format16bppGrayScale i don't think the GDI supports it very well.
Basically if you just create the Bitmap in Format16bppGrayScale, and save it with nothing else, it still gives the error.
I have taken the liberty to rewrite your method Format32bppPArgb
private unsafe static void Main(string[] args)
{
var height = 600;
var width = 600;
var p1 = new Point(0, 0);
var p2 = new Point(width, 0);
var p3 = new Point(width / 2, height);
var r = new Random();
var p = new Point(r.Next(0, width), r.Next(0, width));
using (var im = new Bitmap(width, height, PixelFormat.Format32bppPArgb))
{
var data = im.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
var sc0 = (int*)data.Scan0;
var pLen = sc0 + height * width;
var black = Color.Black.ToArgb();
var white = Color.White.ToArgb();
for (var pI = sc0; pI < pLen; pI++)
*pI = black;
for (long i = 0; i < width * height; i++)
{
Point tp;
switch (r.Next(0, 3))
{
case 0:
tp = new Point((p1.X + p.X) / 2, (p1.Y + p.Y) / 2);
*(sc0 + tp.Y + tp.X * width) = white;
p = tp;
break;
case 1:
tp = new Point((p2.X + p.X) / 2, (p2.Y + p.Y) / 2);
*(sc0 + tp.Y + tp.X * width) = white;
p = tp;
break;
case 2:
tp = new Point((p3.X + p.X) / 2, (p3.Y + p.Y) / 2);
*(sc0 + tp.Y + tp.X * width) = white;
p = tp;
break;
}
}
im.UnlockBits(data);
im.Save(#"D:\img.png", ImageFormat.Png);
}
}
Result
You can convert it after the fact if you want, add pepper and salt to taste
Also if you get rid of all the points and cache the colors this will be a bit faster

Why still not coloring the pixels in acolor using LockBits?

I'm doing a loop over a List pointtocolor contain over 50000 pixels coordinates then i'm trying to color them on the Bitmap bmpBackClouds.
First time i tried with the line:
if ((int)p + (int)(x + y) < bD.Stride * bD.Height)
But then it never step in and never did the p[1] = p[2] = (byte)255; lines.
So now i'm not using this IF and it's doing all the lines.
But in the end i'm getting the same Bitmap as original as it was nothing colored.
float x, y;
bD = bmpBackClouds.LockBits(
new System.Drawing.Rectangle(0, 0, bmpBackClouds.Width, bmpBackClouds.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
IntPtr s0 = bD.Scan0;
unsafe
{
byte* p;
byte* pBU = (byte*)(void*)s0;
for (int i = 0; i < pointtocolor.Count; i++)
{
p = (byte*)(void*)s0;
x = pointtocolor[i].X * (float)currentFactor;
y = pointtocolor[i].Y * (float)currentFactor;
if ((int)x >= bmpBackClouds.Width || (int)y >= bmpBackClouds.Height)
{
continue;
}
x = (int)(y * (float)bD.Stride);
y = (int)(x * 4F);
p += (int)(x + y);
if ((int)p + (int)(x + y) < bD.Stride * bD.Height)
{
if (x + y > 3)
p -= (p - pBU) % 4;
p[1] = p[2] = (byte)255;
p[0] = (byte)0;
p[3] = (byte)255;
}
}
}
bmpBackClouds.UnlockBits(bD);
This is the unsafe code part:
unsafe
{
byte* p;
byte* pBU = (byte*)(void*)s0;
for (int i = 0; i < pointtocolor.Count; i++)
{
//set pointer to the beggining
p = (byte*)(void*)s0;
x = pointtocolor[i].X * (float)currentFactor;
y = pointtocolor[i].Y * (float)currentFactor;
//check if point is inside bmp
if ((int)x >= bmpBackClouds.Width || (int)y >= bmpBackClouds.Height)
{
continue;
}
//Add offset where the point is. The formula: position = Y * stride + X * 4
x = (int)(y * (float)bD.Stride);
y = (int)(x * 4F);
p += (int)(x + y);
//here check, whether the pointer's at a correct position
if (x + y > 3)
p -= (p - pBU) % 4;
//set yellow color
p[1] = p[2] = (byte)255;
p[0] = (byte)0;
p[3] = (byte)255;
}
}
Hans solution is working i just have a small quation the image i get on the hard disk is colored on the right shape but it's opposite or reversed not sure how to call to it what it should be:
On the screenshot on the left is whaht i got on the har disk the bitmap i wrote to with the LockBits.
On the right it's my program and a rectangle i drawed in red and this rectangle should be the colored area on the Bitmap. But on the Bitmap i see this rectangle are but it seems like opposite or reversed.
The question is if there is something wrong with the LockBits code ? Or it seems more like something with my other code ?
(p - pBU) % 4 produces always zero. The difference is always multiple of 4 because each pixel has 4bytes of information. The p pointer jumbs to multiple of 4 from the beginning where pBU points. If you want to test if the points are in the rectangle do this:
GraphicsPath gp = new GraphicsPath();
gp.AddRectangle(new RectangleF(?, ?, ?, ?)); //fill the values of your rectangle
for (int i = 0; i < pointtocolor.Count; i++)
{
if(gp.IsVisible (pointtocolor[i].X * (float)currentFactor, pointtocolor[i].Y * (float)currentFactor)
{
//is inside
}
}
EDIT
When scanning a bitmap and use bD.Stride to see the width using an int pointer is wrong because stride gives color byte count not pixel count
int *p;
p = (int*)(void*)s0;
//x, y the position coordinates
p += y * bD.Stride + x * 4; //wrong
byte *p;
p = (byte*)(void*)s0;
//x, y the position coordinates
p += y * bD.Stride + x * 4; //correct
The error is here
x = (int)(y * (float)bD.Stride);
y = (int)(x * 4F);
You are using previous x to set y!
The correct code is:
float fx, fy;
int x, y;
bD = bmpBackClouds.LockBits(
new System.Drawing.Rectangle(0, 0, bmpBackClouds.Width, bmpBackClouds.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
IntPtr s0 = bD.Scan0;
unsafe
{
byte* p;
//byte* pBU = (byte*)(void*)s0;
for (int i = 0; i < pointtocolor.Count; i++)
{
p = (byte*)(void*)s0;
fx = pointtocolor[i].X * (float)currentFactor;
fy = pointtocolor[i].Y * (float)currentFactor;
if ((int)fx >= bmpBackClouds.Width || (int)fy >= bmpBackClouds.Height)
{
continue;
}
x = (int)fy * bD.Stride;
y = (int)fx * 4;
p += (x + y);
p[1] = p[2] = (byte)255;
p[0] = (byte)0;
p[3] = (byte)255;
}
}
bmpBackClouds.UnlockBits(bD);
valter

How to add noise to image or convert it to 24bpp?

Noob needs help!
I have an image and I need to add noise on it. I tried using AForge libs to do it but this method works only with 24bpp bitmaps and I get something different after resizing. The question is how to convert a bitmap to 24bpp or how to add noise on it? Maybe there are some libs for making this easier.
Resizing:
private Image Fit(Image image)
{
Image img = image;
if (filepath != null)
{
if (img.Width > pictureBox1.Width)
{
double op = ((pictureBox1.Width - (pictureBox1.Width % 100)) % 100) + (pictureBox1.Width % 100) * 0.01;
double percent = img.Width / (pictureBox1.Width * 0.01);
double temp = ((percent - percent % 100 + 100) - percent) * pictureBox1.Height * 0.01;
double height = pictureBox1.Height * 0.01 * ((percent - percent % 100 + 100) - percent);
System.Drawing.Size sz = new Size(pictureBox1.Width, (int)height);
img = resizeImage(img, sz);
}
if (img.Height > pictureBox1.Height)
{
double percent = img.Height / (pictureBox1.Height * 0.01);
double temp = ((percent - percent % 100 + 100) - percent) * pictureBox1.Width * 0.01;
double width = pictureBox1.Width * 0.01 * ((percent - percent % 100 + 100) - percent);
System.Drawing.Size sz = new Size((int)width, pictureBox1.Height);
img = resizeImage(img, sz);
}
}
return img;
}
P.S.> I have a type of bug - system totally refuses to divide 1 by 100 so I had to multiply 1 by 0.01 or I get 0.
Resizing the image to maintain aspect ratio is pretty easy. You compute the horizontal and vertical scaling factors, and then select the smallest of the two.
double vscale = 1.0;
double hscale = 1.0;
if (img.Width > pictureBox1.Width)
{
hscale = (double)pictureBox1.Width/img.Width;
}
if (img.Height > pictureBox1.Height)
{
vscale = (double)pictureBox1.Height/img.Height;
}
double scale = Math.Min(hscale, vscale);
double width = scale * img.Width;
double height = scale * img.Height;
Size sz = new Size((int)width, (int)height);
img = resizeImage(img, sz)
Note that this only scales if the image is larger than the box. It won't zoom the image to make it fit the box if the image is smaller than the box.
Haven't found anything good. That's how I've solved it:
public void GenerateNoise(Image img, int intense)
{
Bitmap finalBmp = img as Bitmap;
Random r = new Random();
int width = img.Width;
int height = img.Height;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
int def = r.Next(0, 100);
if (def < intense)
{
int op = r.Next(0, 1);
if (op == 0)
{
int num = r.Next(0, intense);
Color clr = finalBmp.GetPixel(x, y);
int R = (clr.R + clr.R + num)/2;
if (R > 255) R = 255;
int G = (clr.G + clr.G + num) / 2;
if (G > 255) G = 255;
int B = (clr.B + clr.B + num) / 2;
if (B > 255) B = 255;
Color result = Color.FromArgb(255, R, G, B);
finalBmp.SetPixel(x, y, result);
}
else
{
int num = r.Next(0, intense);
Color clr = finalBmp.GetPixel(x, y);
Color result = Color.FromArgb(255, (clr.R + clr.R - num) / 2, (clr.G + clr.G - num) / 2,
(clr.B + clr.B - num) / 2);
finalBmp.SetPixel(x, y, result);
}
}
}
}
}

How to increase Gaussian Blur strength in Windows Form

I found some great code here on SO and have implemented it in my app and although it is "working", the blur effect is not strong enough to actually realise (without focusing very hard on it) that it has actually blurred anything.
The code is (quite long, sorry):
Bitmap screenshot = null;
void BlurForm()
{
/*
The best time (I assume) is to change the blur when the user as left the window,
as opposed to doing it when they want to open the window, since this may be a
little intensive.
*/
screenshot = null;
blurred.Image = null; // This variable is the PictureBox that's on the Form
screenshot = Screenshot.TakeSnapshot(this);
BitmapFilter.GaussianBlur(screenshot, 4);
blurred.Image = screenshot;
blurred.SendToBack();
}
public class ConvMatrix
{
public int TopLeft = 0, TopMid = 0, TopRight = 0;
public int MidLeft = 0, Pixel = 1, MidRight = 0;
public int BottomLeft = 0, BottomMid = 0, BottomRight = 0;
public int Factor = 1;
public int Offset = 0;
public void SetAll(int nVal)
{
TopLeft = TopMid = TopRight = MidLeft = Pixel = MidRight = BottomLeft = BottomMid = BottomRight = nVal;
}
}
public class BitmapFilter
{
private static bool Conv3x3(Bitmap b, ConvMatrix m)
{
// Avoid divide by zero errors
if (0 == m.Factor) return false;
Bitmap bSrc = (Bitmap)b.Clone();
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData bmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
int stride2 = stride * 2;
System.IntPtr Scan0 = bmData.Scan0;
System.IntPtr SrcScan0 = bmSrc.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
byte* pSrc = (byte*)(void*)SrcScan0;
int nOffset = stride + 6 - b.Width * 3;
int nWidth = b.Width - 2;
int nHeight = b.Height - 2;
int nPixel;
for (int y = 0; y < nHeight; ++y)
{
for (int x = 0; x < nWidth; ++x)
{
nPixel = ((((pSrc[2] * m.TopLeft) + (pSrc[5] * m.TopMid) + (pSrc[8] * m.TopRight) +
(pSrc[2 + stride] * m.MidLeft) + (pSrc[5 + stride] * m.Pixel) + (pSrc[8 + stride] * m.MidRight) +
(pSrc[2 + stride2] * m.BottomLeft) + (pSrc[5 + stride2] * m.BottomMid) + (pSrc[8 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
p[5 + stride] = (byte)nPixel;
nPixel = ((((pSrc[1] * m.TopLeft) + (pSrc[4] * m.TopMid) + (pSrc[7] * m.TopRight) +
(pSrc[1 + stride] * m.MidLeft) + (pSrc[4 + stride] * m.Pixel) + (pSrc[7 + stride] * m.MidRight) +
(pSrc[1 + stride2] * m.BottomLeft) + (pSrc[4 + stride2] * m.BottomMid) + (pSrc[7 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
p[4 + stride] = (byte)nPixel;
nPixel = ((((pSrc[0] * m.TopLeft) + (pSrc[3] * m.TopMid) + (pSrc[6] * m.TopRight) +
(pSrc[0 + stride] * m.MidLeft) + (pSrc[3 + stride] * m.Pixel) + (pSrc[6 + stride] * m.MidRight) +
(pSrc[0 + stride2] * m.BottomLeft) + (pSrc[3 + stride2] * m.BottomMid) + (pSrc[6 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
p[3 + stride] = (byte)nPixel;
p += 3;
pSrc += 3;
}
p += nOffset;
pSrc += nOffset;
}
}
b.UnlockBits(bmData);
bSrc.UnlockBits(bmSrc);
return true;
}
public static bool GaussianBlur(Bitmap b, int nWeight /* default to 4*/)
{
ConvMatrix m = new ConvMatrix();
m.SetAll(1);
m.Pixel = nWeight;
m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 2;
m.Factor = nWeight + 12;
return BitmapFilter.Conv3x3(b, m);
}
}
class Screenshot
{
public static Bitmap TakeSnapshot(Control ctl)
{
Bitmap bmp = new Bitmap(ctl.Size.Width, ctl.Size.Height);
using (Graphics g = System.Drawing.Graphics.FromImage(bmp))
{
g.CopyFromScreen(
ctl.PointToScreen(ctl.ClientRectangle.Location),
new Point(0, 0), ctl.ClientRectangle.Size
);
}
return bmp;
}
}
I've tried changing every single value in there and it doesn't matter which value(s) I change, I just can't get it to have a stronger blur effect. How can I increase the strength of the Gaussian Blur?
The Form is 80% Opaque, so the blur effect you would see is the windows wallpaper or whatever is behind the form - but blurred. Only problem (as I've said) is that the blur is not strong at all.
After just glancing at the code, it looks to me like the blur filter is hardcoded to only operate in a 3x3 area at a time, severely restricting how blurred it can make an image.
Unless I'm mistaken, the only way to significantly increase the amount of blurring is to find a more sophisticated implementation.

XNA 2D TileEngine Mouse clicking issue

My problem is that clicks only get registers on the lower right corner and in some cases not even there it seems to get worse the longer you stray from the 0.0 and the likes the worse it gets.
public void Render(SpriteBatch B, Camera C)
{
Vector2 firstSquare = new Vector2(C.Position.X / 32, C.Position.Y / 32);
int firstX = (int)firstSquare.X;
int firstY = (int)firstSquare.Y;
Vector2 squareOffset = new Vector2(C.Position.X % 32, C.Position.Y % 32);
int offsetX = (int)squareOffset.X;
int offsetY = (int)squareOffset.Y;
for (int y = 0; y < 16; y++)
{
for (int x = 0; x < 26; x++)
{
Tile T = GetTile(x + firstX, y + firstY);
if (T == null)
{
continue;
}
T.RenderWithCamera(B,new Vector2((x*32)-offsetX,(y*32)-offsetY));
}
}
public void CheckClick(float mx, float my,Camera C)
{
Vector2 firstSquare = new Vector2(C.Position.X / 32, C.Position.Y / 32);
int x = (int)firstSquare.X;
int y = (int)firstSquare.Y;
Vector2 squareOffset = new Vector2(C.Position.X % 32, C.Position.Y % 32);
int offsetX = (int)squareOffset.X;
int offsetY = (int)squareOffset.Y;
int vx = (int)mx / 32;
int vy = (int)my / 32;
float x1 = vx + x;
float y1 = vy + y;
int maxX, maxY;
maxX = C.Width / 32;
maxY = C.Height / 32;
Console.WriteLine("MAX_X:" + maxX + "MAX_Y:" + maxY);
Tile T = GetTile(x1, y1);
Rectangle A = new Rectangle((int)mx, (int)my, 1, 1);
if (T == null)
{ Console.WriteLine("No Tile found"); return; }
if (T.IsInside(A))
{
Console.WriteLine("Not inside?");
Tile S = null;
S = new Wall((int)x1, (int)y1, 0);
if (S != null)
{
tiles.Add(S);
tiles2[(int)T.pos.X, (int)T.pos.Y] = S;
}
}
Console.WriteLine("Clicked Tile at X:" + T.pos.X + "Y:" + T.pos.Y);
}
public bool IsInside(Rectangle B) // TILE
{
Rectangle rectA = new Rectangle((int)Last_pos.X, (int)Last_pos.Y, icon.Width, icon.Height);
Console.WriteLine("A:" + rectA.X + "A.y:" + rectA.Y + "B.X:" + B.X + "B.Y:" + B.Y);
if(rectA.Intersects(B))
{
return true;
}
else
return false;
}
Here's how I like to handle clicking a tilemap.
int xTile = Math.floor((Mouse.X + CameraBounds.left) / Tile.width);
int yTile = Math.floor((Mouse.Y + CameraBounds.top) / Tile.height);

Categories

Resources