quinta-feira, 29 de novembro de 2012

Saving Images Bytes

Ok so now we need to be able to save all the existing images from a folder... So we came up with a simple code to help us on this hard and time consuming task... and it goes a little something like this :)

private void ImportImageToDatabase()
{
      //Get a path from the app.config app file
      string path = ConfigurationManager.AppSettings.Get("ImagesPath");

      foreach (var folder in Directory.GetDirectories(path))
      {
            //This variable will be needed so it's easier to get the folder name instead of having to do string operations
           DirectoryInfo d = new DirectoryInfo(folder);
           foreach (var file in Directory.GetFiles(folder))
          {
               //This variable will be needed so it's easier to get the file name instead of having to do string operations
               FileInfo f = new FileInfo(file);

               //Now we can call the method we previously created to get the image bytes and save them
               Byte[] image = GetImageBytes(file);
               //Save image to the database and we can get the name of the file by using f.Name
          }
    }
}

Handle Images

Today we're going to implement a way to save the images without having to have the actual image file.
We'll create a program that will look to a specified folder chosen by the user, then we will import the bytes of each image to a database, storing the bytes of the image and its name.
The code to get the bytes is very simple as you can see below:


private Byte[] GetImageBytes(string pathToImage)
        {
            if (File.Exists(pathToImage))
                return File.ReadAllBytes(pathToImage);

            return null;
        }

And then will be able to use those bytes to generate an image with the following code:


using (MemoryStream stream = new MemoryStream(GetImageBytes(SomePath)))
{
        pictureBox1.Image = System.Drawing.Image.FromStream(stream);
}

We can use this to prevent us from having a space consuming app.

quarta-feira, 28 de novembro de 2012

The Beginning!

We're going to try to develop a Magic The Gathering game. Today we'll start thinking about the base structure of the game, classes and interfaces.
Keeping in mind we have to try and make it as easy to upgrade as possible :)