Create a green color with an alpha component then draw a green rectangle to the bitmap in memory

image_pdfimage_print

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

protected override void OnPaint(PaintEventArgs e) {
Graphics gForm = e.Graphics;
gForm.FillRectangle(Brushes.White, this.ClientRectangle);
for (int i = 1; i <= 7; ++i) { Rectangle r = new Rectangle(i * 40 - 15, 0, 15, this.ClientRectangle.Height); gForm.FillRectangle(Brushes.Orange, r); } Bitmap bmp = new Bitmap(260, 260, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics gBmp = Graphics.FromImage(bmp); Color green = Color.FromArgb(0x40, 0, 0xff, 0); Brush greenBrush = new SolidBrush(green); gBmp.FillRectangle(greenBrush, 10, 10, 140, 140); gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height); bmp.Dispose(); gBmp.Dispose(); greenBrush.Dispose(); } public static void Main() { Application.Run(new Form1()); } } [/csharp]

Create a red color with an alpha component then draw a red circle to the bitmap in memory

image_pdfimage_print

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

protected override void OnPaint(PaintEventArgs e) {
Graphics gForm = e.Graphics;
gForm.FillRectangle(Brushes.White, this.ClientRectangle);
for (int i = 1; i <= 7; ++i) { Rectangle r = new Rectangle(i * 40 - 15, 0, 15,this.ClientRectangle.Height); gForm.FillRectangle(Brushes.Orange, r); } Bitmap bmp = new Bitmap(260, 260,System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics gBmp = Graphics.FromImage(bmp); Color red = Color.FromArgb(0x60, 0xff, 0, 0); Brush redBrush = new SolidBrush(red); gBmp.FillEllipse(redBrush, 70, 70, 160, 160); gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height); bmp.Dispose(); gBmp.Dispose(); redBrush.Dispose(); } public static void Main() { Application.Run(new Form1()); } } [/csharp]

Create a Bitmap image in memory and set its CompositingMode

image_pdfimage_print

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

protected override void OnPaint(PaintEventArgs e) {
Graphics gForm = e.Graphics;
gForm.FillRectangle(Brushes.White, this.ClientRectangle);
for (int i = 1; i <= 7; ++i) { Rectangle r = new Rectangle(i * 40 - 15, 0, 15, this.ClientRectangle.Height); gForm.FillRectangle(Brushes.Orange, r); } Bitmap bmp = new Bitmap(260, 260,System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics gBmp = Graphics.FromImage(bmp); gBmp.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height); bmp.Dispose(); gBmp.Dispose(); } public static void Main() { Application.Run(new Form1()); } } [/csharp]

Use a color matrix to change the color properties of the image

image_pdfimage_print
   
  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Drawing.Printing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
        Bitmap bmp = new Bitmap("alphabet.gif");
        Graphics g = e.Graphics;

        float[][] matrixItems = {
                    new float[] {0.2f, 0, 0, 0, 0},
                    new float[] {0, 0.8f, 0, 0, 0},
                    new float[] {0, 0, 1, 0, 0},
                    new float[] {0, 0, 0, 1, 0}, 
                    new float[] {0, 0, 0, 0, 1}};
        ColorMatrix colorMatrix = new ColorMatrix(matrixItems);

        ImageAttributes imageAtt = new ImageAttributes();
        imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

        TextureBrush tb = new TextureBrush(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), imageAtt);
        tb.WrapMode = WrapMode.Tile;
        g.FillRectangle(tb, this.ClientRectangle);
        bmp.Dispose();
        tb.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

   
     


Bitmap.HorizontalResolution

image_pdfimage_print
   
  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    Bitmap bmp = new Bitmap("rama.jpg");
    g.DrawImage(bmp, 0, 0);

    Console.WriteLine("Screen resolution: " + g.DpiX + "DPI");
    Console.WriteLine("Image resolution: " + bmp.HorizontalResolution + "DPI");
    Console.WriteLine("Image Width: " + bmp.Width);
    Console.WriteLine("Image Height: " + bmp.Height);

    SizeF s = new SizeF(bmp.Width * (g.DpiX / bmp.HorizontalResolution),
              bmp.Height * (g.DpiY / bmp.VerticalResolution));
    Console.WriteLine("Display size of image: " + s);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

   
     


Read Bitmap Size by using BinaryReader

image_pdfimage_print
   
  


using System;
using System.IO;


class Class1 {
    static void Main(string[] args) {
        string[] cma = Environment.GetCommandLineArgs();

        if (cma.GetUpperBound(0) >= 1) {
            FileStream myFStream = new FileStream(cma[1], FileMode.Open, FileAccess.Read);
            BinaryReader binRead = new BinaryReader(myFStream);
            binRead.BaseStream.Position = 0x12;
            
            Console.WriteLine(binRead.ReadInt32());
            Console.WriteLine(binRead.ReadInt32());
            Console.WriteLine(binRead.ReadInt16());
            Console.WriteLine(binRead.ReadInt16());

            binRead.Close();
            myFStream.Close();
        }
    }
}

   
     


new Bitmap(bitmap, size)

image_pdfimage_print
   
  


using System;
using System.Drawing;
using System.Windows.Forms;

class HelloWorldBitmap : Form {
    const float fResolution = 300;
    Bitmap bitmap = new Bitmap(1, 1);

    public static void Main() {
        Application.Run(new HelloWorldBitmap());
    }
    public HelloWorldBitmap() {
        Text = "Hello, World!";
        ResizeRedraw = true;

        bitmap.SetResolution(fResolution, fResolution);

        Graphics grfx = Graphics.FromImage(bitmap);
        Font font = new Font("Times New Roman", 72);
        Size size = grfx.MeasureString(Text, font).ToSize();

        bitmap = new Bitmap(bitmap, size);
        bitmap.SetResolution(fResolution, fResolution);

        grfx = Graphics.FromImage(bitmap);
        grfx.Clear(Color.White);
        grfx.DrawString(Text, font, Brushes.Black, 0, 0);
        grfx.Dispose();
    }
    protected override void OnPaint(PaintEventArgs pea) {
        pea.Graphics.DrawImage(bitmap, 0, 0);
    }
}