using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // Open a File Dialog to select an image file OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.bmp;*.jpg;*.jpeg;*.png"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { // Set the Image property of the PictureBox to the selected image file pictureBox1.Image = new Bitmap(openFileDialog1.FileName); } } } }
using System; using System.Drawing; namespace ConsoleApp1 { class Program { static void Main(string[] args) { // Create a new Bitmap of size 500x500px Bitmap bmp = new Bitmap(500, 500); // Create a Graphics object from the Bitmap Graphics g = Graphics.FromImage(bmp); // Draw a blue rectangle on the Bitmap g.FillRectangle(Brushes.Blue, new Rectangle(100, 100, 300, 300)); // Save the Bitmap to a file bmp.Save(@"C:\test\image.png", System.Drawing.Imaging.ImageFormat.Png); } } }This code creates a new Bitmap of size 500x500px and draws a blue rectangle on it using the Graphics object. It then saves the Bitmap to a file. This uses the System.Drawing package.