Rotate image in c# - c#

I wish the image in picture box could flip when key left is pressed, as the code shown below, but it didn't flip when key left is pressed. Anyone could help ? Thanks a lot!
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
KeyDown += new KeyEventHandler(Form1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.Right)
{
x += 10;
}
else if (e.KeyCode == Keys.Left)
{
pictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipX);
pictureBox1.Refresh();
x -= 10;
}
else if (e.KeyCode == Keys.Up)
{
y -= 10;
}
pictureBox1.Location = new Point (x, y);
pictureBox1.Invalidate();
}
}
}

Instead of Image you are trying rotating the picture box. As per the example given in the link. rotate the image then assign it to picture box.
Refer this link officially from Microsoft docs https://msdn.microsoft.com/en-us/library/system.drawing.image.rotateflip(v=vs.110).aspx

Related

Move the label from left to right and right to left

My question is very simple.
My Label1 should move continuously from left to right.
(Start from the left part of the form and go to the right part of the form. When it reaches the right part of the Form, go to the left and so on.) I have 2 timers(one for right and one for left)
I have the following code, it starts at the left and goes to the right, but when it reaches the right of the Form, it doesn't return.
Can anyone help me?
enum Position
{
Left,Right
}
System.Windows.Forms.Timer timerfirst = new System.Windows.Forms.Timer();
private int _x;
private int _y;
private Position _objPosition;
public Form1()
{
InitializeComponent();
if (label1.Location == new Point(0,180))
{
_objPosition = Position.Right;
}
if (label1.Location == new Point(690,0))
{
_objPosition = Position.Left;
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Label1.SetBounds(_x, _y, 0, 180);
}
private void tmrMoving_Tick(object sender, EventArgs e)
{
tmrMoving.Start();
if (_objPosition == Position.Right && _x < 710)
{
_x +=10;
}
if(_x == 710)
{
tmrMoving.Stop();
}
Invalidate();
}
private void tmrMoving2_Tick(object sender, EventArgs e)
{
tmrMoving2.Start();
if (_objPosition == Position.Right && _x > 690)
{
_x -= 10;
}
if(_x == 1)
{
tmrMoving2.Stop();
}
Invalidate();
}
Thanks.
I think you've overcomplicated things. Let's just have one timer, a step number of pixels that we sometimes flip negative, and some time later flip back to positive. We can move the label by repeatedly setting its Left:
public partial class Form1 : Form
{
private int _step = 10;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (label1.Right > this.Width) _step = -10;
if (label1.Left < 0) _step = 10;
label1.Left += _step;
}
}
You might want to fine tune it, but the basic motion is there

C# End image movement by pressing key

I am new to C# and wanted to know how I could make it so that once the user presses the enter key, the current location of the image becomes its fixed location. I was thinking that the best way to do it would be using a while loop. Help would really be appreciated. The following is my code for moving my image:
private void pictureBox1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
{
if (e.KeyCode == Keys.Right) x += 50;
else if (e.KeyCode == Keys.Left) x -= 50;
else if (e.KeyCode == Keys.Up) y -= 50;
else if (e.KeyCode == Keys.Down) y += 50;
pictureBox1.Location = new Point(x, y);
}
}
In this solution i have used a global bool object and changed flag to true once an enter key is pressed.
I have a form with Picture Box and on the form_KeyDown event i placed your code with small change.
bool bIsEnterKeyPressed = false;
private void frmSampleJson_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
bIsEnterKeyPressed = true;
}
if (!bIsEnterKeyPressed)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
{
if (e.KeyCode == Keys.Right) x += 50;
else if (e.KeyCode == Keys.Left) x -= 50;
else if (e.KeyCode == Keys.Up) y -= 50;
else if (e.KeyCode == Keys.Down) y += 50;
pictureBox1.Location = new Point(x, y);
}
}
}
Once enter is pressed the bIsEnterKeyPressed is changed to true and the position will not be changed after that.

Collision only works 2 sides

So i am trying to make a pacman clone in c#, Visual studio express 2013. I have the maze, and ive put labels on the edges. I have a point with the future coordinates of pacman. If that point doesnt intersect with the labels, pacmans location will take the location of the point. This works just fine when pacman is comming from down or right, however it doesnt work if he comes from right or top. This is also true for the ghosts, whose movement is randomly generated. They both seem to consider the label bigger. I tried to search the internet for why this might be, but i cant seem to find an answer. Ive put down the part with collision check and pacman movement. Thanks in advance
private void timer_Tick(object sender, EventArgs e)
{
//colision check
PictureBox intermed = new PictureBox
{
Location = new Point(pictureBox1.Location.X + pacdirx, pictureBox1.Location.Y + pacdiry),
};
if (!intermed.Bounds.IntersectsWith(label1.Bounds) )
pictureBox1.Location = new Point(pictureBox1.Location.X + pacdirx, pictureBox1.Location.Y + pacdiry);
private void Form3_KeyDown(object sender, KeyEventArgs e)
{
//pacman movement
if (e.KeyCode == Keys.W)
{
pacdiry = -3;
pacdirx = 0;
pictureBox1.Image = Image.FromFile("eat_up.gif");
}
if (e.KeyCode == Keys.S)
{
pacdiry = 3;
pacdirx = 0;
pictureBox1.Image = Image.FromFile("eat_down.gif");
}
if (e.KeyCode == Keys.D)
{
pacdiry = 0;
pacdirx = 3;
pictureBox1.Image = Image.FromFile("eat_right.gif");
}
if (e.KeyCode == Keys.A)
{
pacdiry = 0;
pacdirx = -3;
pictureBox1.Image = Image.FromFile("eat_left.gif");
}
}

PictureBox select switchnig with arrow keys

I need help I have a set PictureBox (40), and I need to select these pictureboxes with arrows. I mean when I'm on the first picture and press right arrow key (border changing - selected), the border of the first should switch to none and next one switch to border FixedSingle.
One idea is:
if (keyData == Keys.Right) {
if (PictureBox1.BorderStyle == BorderStyle.FixedSingle) {
PictureBox1.BorderStyle = BorderStyle.None;
PictureBox2.BorderStyle = BorderStyle.FixedSingle;
} else if (PictureBox2.BorderStyle == BorderStyle.FixedSingle) {
pictu.....
}
}
but this method takes too much time so I'm looking for a simpler method.
Can somebody help me find a simpler way to do this?
EDIT new code:
namespace testPics
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown_1(object sender, KeyEventArgs e)
{
changePictureBox(e.KeyData);
}
List<PictureBox> myPictureBoxes;
int index;
public void iniPictureBoxes()
{
myPictureBoxes = new List<PictureBox>();
myPictureBoxes.Add(pictureBox1);
myPictureBoxes.Add(pictureBox2);
myPictureBoxes.Add(pictureBox3);
index = 0;
}
public void changePictureBox(Keys keyData)
{
myPictureBoxes[index].BorderStyle = BorderStyle.None;
if (keyData == Keys.Right)
{
if (index < myPictureBoxes.Count - 1)
index++;
}
else if (keyData == Keys.Left)
{
if (index > 0)
index--;
}
myPictureBoxes[index].BorderStyle = BorderStyle.FixedSingle;
}}}
You could create a list of pictureboxes.
Then for example you can add an indexer (just to keep it simple).
The indexer is an int (or in your case can be a byte) that stores the index of the currently selected picturebox.
If the user presses "right arrow" key just change the border of the current indexed picturebox increment the indexer and update "the now indexd picturebox".
List<PictureBox> myPictureBoxes;
int index;
public void iniPictureBoxes()
{
myPictureBoxes = new List<PictureBox>();
myPictureBoxes.Add(pictureBox1);
myPictureBoxes.Add(pictureBox2);
index = 0;
}
public void changePictureBox(Keys keyData)
{
myPictureBoxes[index].BorderStyle = BorderStyle.None;
if(keyData == Keys.Right)
{
if(index < myPictureBoxes.Count - 1)
index++;
}
else if(keyData == Keys.Left)
{
if(index>0)
index--;
}
myPictureBoxes[index].BorderStyle = BorderStyle.FixedSingle;
}
this just sets the borderstyle. If you want to really select the picturebox you also need to implement it (picturebox.select();)
It may be better to create the pictureboxes generically. So you don't have to do add all of them manually to the List.
Here is the generic code for adding pictureboxes (in this case 5):
public void iniPictureBoxes()
{
myPictureBoxes = new List<PictureBox>();
for (int i = 0; i < 5; i++)
{
PictureBox tempPB = new PictureBox();
tempPB.Location = new Point(i * 15, 10);
tempPB.Size = new Size(10, 10);
tempPB.BackColor = Color.Black;
Controls.Add(tempPB);
myPictureBoxes.Add(tempPB);
}
index = 0;
}
and here the way to add an event: just double click the event u want to have.
Then a method is being auto generated for you. And you should change it to
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
changePictureBox(e.KeyData);
}

Flip Image(Mirror) in a PictureBox Control

First of all I am a beginner with C#.
I have a picturebox and a timer (enabled, interval = 25).
I have inserted a gif image of a bird in the picturebox.
And in the Timer event I have written,
bool positionBird = true;
private void timer1_Tick(object sender, EventArgs e)
{
if (PictureBox1.Location.X == Screen.PrimaryScreen.Bounds.Width)
{
positionBird = false;
}
else if (PictureBox1.Location.X == 0)
{
positionBird = true;
}
if(positionBird)
{
PictureBox1.Left += 1;
}
else
{
PictureBox1.Left += -1;
}
}
But what I want to achieve is, when the picture box touches the right
boundary and condition become false, I want to flip the image of bird in
the picturebox. Right now the bird is doing Michael Jackson's Moonwalk!
I tried to flip the bird (mirror the bird) using the below code.
else
{
PictureBox pict = new PictureBox();
pict = PictureBox1;
pict.Image.RotateFlip(RotateFlipType.RotateNoneFlipX);
pict.Left += -1;
}
But it looks weird. It shows the flip image and normal image both. Can
someone help me on this? As I already said I am a beginner. Some simple
code with explanation would be very helpful. Also can someone tell me
what I am doing wrong?
DO NOT CREATE another Picture Box. You are seeing the original picture because you have not modified the original but the newly created one.
So the modified code is follows:
bool positionBird = true;
private void timer1_Tick(object sender, EventArgs e)
{
if (PictureBox1.Location.X == Screen.PrimaryScreen.Bounds.Width)
{
positionBird = false;
PictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipX); // picture flips only once when touches boundary
}
else if (PictureBox1.Location.X == 0)
{
positionBird = true;
PictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipX); // picture flips only once when touches boundary
}
if(positionBird)
{
PictureBox1.Left += 1;
}
else
{
PictureBox1.Left += -1;
}
}

Categories