Hi all
I have been playing around with some basic graphic elements to get in touch with OOP and C#. i have managed to put the following piece of code together but am experiencing an unusual issue with it.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Graphictest1
{
public partial class Form1 : Form
{
public int xpos = 10;
public int ypos = 10;
Bitmap myBitmap = new Bitmap(@"c:\green_ball.bmp"); //64 x 64 pixel images
Bitmap myBitmap1 = new Bitmap(@"c:\wall1.bmp");
Bitmap myBitmap2 = new Bitmap(@"c:\brown_ball.bmp");
public Graphics g;
private Bitmap myscreen;
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.UpdateStyles();
myscreen = draw();
this.Invalidate();
}
public Bitmap draw()
{
Bitmap back = new Bitmap(this.Width, this.Height);
Graphics p = Graphics.FromImage(back);
int x;
int y;
int tile = 1;
myBitmap.MakeTransparent(Color.White);
myBitmap1.MakeTransparent(Color.White);
myBitmap2.MakeTransparent(Color.White);
for (y = 0; y <= 640; y = y + 64)
{
for (x = 0; x <= 768; x = x + 64)
{
if (tile == 1)
{
p.DrawImage(myBitmap1, x, y);
tile = 0;
}
else
{
p.DrawImage(myBitmap2, x, y);
tile = 1;
}
}
}
p.DrawImage(myBitmap, xpos, ypos);
return (back) ;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
string key = e.KeyData.ToString();
if (key == "Right")
{
xpos = xpos +4;
}
else if (key == "Left")
{
xpos = xpos -4;
}
else if (key == "Up")
{
ypos = ypos -4;
}
else if (key == "Down")
ypos = ypos +4;
myscreen = draw();
this.Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
// its overridden
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(this.myscreen, 0, 0);
//base.OnPaint(e);
}
}
}
Nothing spectacular in there as you can see (well to someone who is used to it anyway - which im not).
My problem is this, in the draw routine, the three lines which turn on transparency (highlighted) seem to affect the actual size of the graphics the system draws.
Is it supposed to affect the size ?
when i comment them out, i get graphics about 1/4 of the dimension i get when uncommented. Can anyone shed any light on this ?
it does not affect the actual execution of the code in anyother way i can see.
thanks in advance.