Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#
Tip/Trick

Having Fun with Image Creation in C# - Part 1

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
10 Dec 2015CPOL 19.2K   254   11   5
Exploring ways to create abstract images in C#

Introduction

This tip is intended to demonstrate how to create or manipulate images in C#.

Background

You just need to know how pixels are made and how to think of images in terms of pixels and using loops of them.

Using the Code

The Imaging SDK is just to hold different image size configurations and effects n presets (Just METADATA).

The Application Layout is as follows:

The trackbars in order are as follows:

  1. Red
  2. Blue
  3. Green

The following code demonstrates how to play with the images.

C#
using ImagingSDK.Configuration;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace Imaging
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void rndBtn_Click( object sender, EventArgs e )
        {
            //disabling the button just to make sure any 
            rndBtn.Enabled = false;

            //setting up the configuration for the image.
            ImageSizeConfiguration Config = 
            new ImageSizeConfiguration(ImageSizeConfiguration.SizeConfig.S_720p);
            ImagingSDK.ImageProcessor imgPrcsr = 
            new ImagingSDK.ImageProcessor(@"C://1.bmp", Config);

            //Instantiating the image.
            Bitmap image = new Bitmap(imgPrcsr.sizeConfig.width, imgPrcsr.sizeConfig.height);

            //the rbga integers for pixels.
            int a, r, b, g, x2, y2;

            //Random number generator for pixels
            Random rand = new Random();

            //width for bars of colors.
            int Width = 5, PrevX = 0;

            //Color to apply to pixels
            Color c = Color.FromArgb(rand.Next(0, 255), 
			RedTrack.Value, GreenTrack.Value, BlueTrack.Value);

            //looping thru the pixels
            for (int x = 0; x < imgPrcsr.sizeConfig.width; x++)
            {
                if (x - PrevX == Width)
                {
                    PrevX = x;
                    //create random pixels
                    a = rand.Next(100, 200);
                    r = RedTrack.Value;
                    g = GreenTrack.Value;
                    b = BlueTrack.Value;

                    //generate color from random ARGB values
                    c = Color.FromArgb(a, r, g, b);
                }

                for (int y = 0; y < imgPrcsr.sizeConfig.height; y++)
                {
                    //Setting each pixel in the bitmap.
                    image.SetPixel(x, y, c);
                }
            }
            
            image.Save("1.png", ImageFormat.Png);

            //setting the image in the picture box.
            Viewer.Image = image;

            //Enabling the button again.
            rndBtn.Enabled = true;
        }

        private void loadBtn_Click( object sender, EventArgs e )
        {

        }
    }
}    

Results:

Get the source code from here.

Points of Interest

I've learnt how to deal with images in this project. It was a great learning curve for me.

History

List of articles in this series:

  1. Current article
  2. Having Fun With Image Creation - Part 2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCannot see the images Pin
leiyangge29-Nov-15 14:30
leiyangge29-Nov-15 14:30 
AnswerRe: Cannot see the images Pin
Akash Gutha29-Nov-15 14:52
professionalAkash Gutha29-Nov-15 14:52 
GeneralRe: Cannot see the images Pin
leiyangge29-Nov-15 15:58
leiyangge29-Nov-15 15:58 
Still same issue.
GeneralRe: Cannot see the images Pin
Akash Gutha29-Nov-15 15:59
professionalAkash Gutha29-Nov-15 15:59 
GeneralRe: Cannot see the images Pin
leiyangge29-Nov-15 18:38
leiyangge29-Nov-15 18:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.