Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions snippets/csharp/System.Drawing/Bitmap/.ctor/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

internal static class Program
{
[STAThread]
private static void Main()
{
//BitmapConstructorForm1.Run();
//BitmapConstructorForm11.Run();
BitmapConstructorForm12.Run();
}
}
9 changes: 9 additions & 0 deletions snippets/csharp/System.Drawing/Bitmap/.ctor/Project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

</Project>
200 changes: 98 additions & 102 deletions snippets/csharp/System.Drawing/Bitmap/.ctor/form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,85 @@
using System.Drawing;
using System.Windows.Forms;

public class Form1:
System.Windows.Forms.Form
public class BitmapConstructorForm1 : Form

{
#region " Windows Form Designer generated code "

public Form1() : base()
{

//This call is required by the Windows Form Designer.
public BitmapConstructorForm1() : base()
Comment thread
gewarren marked this conversation as resolved.
{
// This call is required by the Windows Form Designer.
InitializeComponent();
this.Button1.Click += new EventHandler(Button1_Click);
this.Button2.Click += new EventHandler(Button2_Click);
InitializeBitmap();
Button1.Click += new EventHandler(Button1_Click);
Button2.Click += new EventHandler(Button2_Click);

string filePath = "path//to//your//image.bmp";
InitializeBitmap(filePath);
InitializeStreamBitmap();

//Add any initialization after the InitializeComponent() call
// Add any initialization after the InitializeComponent() call.
}

//Form overrides dispose to clean up the component list.
// Form overrides Dispose to clean up the component list.
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
components?.Dispose();
}
base.Dispose(disposing);
}

//Required by the Windows Form Designer
private System.ComponentModel.IContainer components;
// Required by the Windows Form Designer.
private System.ComponentModel.IContainer components = null;

//NOTE: The following procedure is required by the Windows Form Designer
//It can be modified using the Windows Form Designer.
//Do not modify it using the code editor.
internal System.Windows.Forms.Button Button1;
internal System.Windows.Forms.PictureBox PictureBox1;
internal System.Windows.Forms.Button Button2;
// NOTE: The following procedure is required by the Windows Form Designer.
// It can be modified using the Windows Form Designer.
// Do not modify it using the code editor.
internal Button Button1;
internal PictureBox PictureBox1;
internal Button Button2;

[System.Diagnostics.DebuggerStepThrough]
private void InitializeComponent()
{
this.Button1 = new System.Windows.Forms.Button();
this.PictureBox1 = new System.Windows.Forms.PictureBox();
this.Button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
Button1 = new Button();
PictureBox1 = new PictureBox();
Button2 = new Button();
SuspendLayout();
//
// Button1
//
this.Button1.Location = new System.Drawing.Point(24, 192);
this.Button1.Name = "Button1";
this.Button1.Size = new System.Drawing.Size(96, 23);
this.Button1.TabIndex = 2;
this.Button1.Text = "Rotate and Flip";
Button1.Location = new Point(24, 192);
Button1.Name = "Button1";
Button1.Size = new Size(96, 23);
Button1.TabIndex = 2;
Button1.Text = "Rotate and Flip";
//
// PictureBox1
//
this.PictureBox1.Location = new System.Drawing.Point(48, 40);
this.PictureBox1.Name = "PictureBox1";
this.PictureBox1.Size = new System.Drawing.Size(168, 72);
this.PictureBox1.TabIndex = 3;
this.PictureBox1.TabStop = false;
PictureBox1.Location = new Point(48, 40);
PictureBox1.Name = "PictureBox1";
PictureBox1.Size = new Size(168, 72);
PictureBox1.TabIndex = 3;
PictureBox1.TabStop = false;
//
// Button2
//
this.Button2.Location = new System.Drawing.Point(152, 192);
this.Button2.Name = "Button2";
this.Button2.TabIndex = 4;
this.Button2.Text = "Button2";
Button2.Location = new Point(152, 192);
Button2.Name = "Button2";
Button2.TabIndex = 4;
Button2.Text = "Button2";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.Button2);
this.Controls.Add(this.PictureBox1);
this.Controls.Add(this.Button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
ClientSize = new Size(292, 266);
Controls.Add(Button2);
Controls.Add(PictureBox1);
Controls.Add(Button1);
Name = "Form1";
Text = "Form1";
ResumeLayout(false);
}

#endregion

// <snippet3Intro>
// The following code example demonstrates constructing how to construct
// a new Bitmap from a file.
Expand Down Expand Up @@ -116,24 +109,22 @@ private void InitializeComponent()
//<snippet4>
Bitmap bitmap1;

private void InitializeBitmap()
private void InitializeBitmap(string filePath)
{
try
{
bitmap1 = (Bitmap)Bitmap.FromFile(@"C:\Documents and Settings\" +
@"All Users\Documents\My Music\music.bmp");
bitmap1 = (Bitmap)Bitmap.FromFile(filePath);
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
PictureBox1.Image = bitmap1;
}
catch(System.IO.FileNotFoundException)
catch (System.IO.FileNotFoundException)
{
MessageBox.Show("There was an error." +
"Check the path to the bitmap.");
MessageBox.Show("There was an error. Check the path to the bitmap.");
}
}
//</snippet4>

private void Button1_Click(System.Object sender, System.EventArgs e)
private void Button1_Click(object sender, EventArgs e)
{

if (bitmap1 != null)
Expand All @@ -153,11 +144,10 @@ private void Button1_Click(System.Object sender, System.EventArgs e)
// a form that contains a button named Button2. Paste the code into the
// form and associate this method with the button's Click event.
//<snippet1>
private void Button2_Click(System.Object sender, System.EventArgs e)
private void Button2_Click(object sender, EventArgs e)
{

Bitmap bitmap1 = Bitmap.FromHicon(SystemIcons.Hand.Handle);
Graphics formGraphics = this.CreateGraphics();
Graphics formGraphics = CreateGraphics();
GraphicsUnit units = GraphicsUnit.Point;

RectangleF bmpRectangleF = bitmap1.GetBounds(ref units);
Expand All @@ -179,70 +169,76 @@ private void InitializeStreamBitmap()
{
try
{
System.Net.WebRequest request =
System.Net.WebRequest.Create(
"http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif");
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream responseStream =
response.GetResponseStream();
Bitmap bitmap2 = new Bitmap(responseStream);
using System.Net.Http.HttpClient client = new();
using System.Net.Http.HttpResponseMessage response =
client.GetAsync("http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif")
.GetAwaiter()
.GetResult();
response.EnsureSuccessStatusCode();
using System.IO.Stream responseStream =
response.Content.ReadAsStreamAsync()
.GetAwaiter()
.GetResult();
Bitmap bitmap2 = new(responseStream);
PictureBox1.Image = bitmap2;
}
catch(System.Net.WebException)
catch (System.Net.Http.HttpRequestException)
{
MessageBox.Show("There was an error opening the image file."
+ "Check the URL");
MessageBox.Show("There was an error opening the image file. Check the URL");
}
}
//</snippet2>
// The following code example demonstrates how to use the Image.PixelFormat,

// The following code example demonstrates how to use the Image.PixelFormat,
// Image.Height, Image.Width, and BitmapData.Scan0 properties; the Bitmap.LockBits
// and Bitmap.UnlockBits methods; and the ImageLockMode enumeration.
// This example is designed to be used with Windows
// Forms. To run this example, paste it into a form and handle the form's Paint event by
// calling the LockUnlockBitsExample method, passing e as PaintEventArgs.
// This example assumes the existence of an 24bpp image file named fakePhoto.jpg at c:\.

//<snippet5>
private void LockUnlockBitsExample(PaintEventArgs e)
{
private static void LockUnlockBitsExample(PaintEventArgs e)
{
// Create a new bitmap.
Bitmap bmp = new("c:\\fakePhoto.jpg");

// Create a new bitmap.
Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
// Lock the bitmap's bits.
Rectangle rect = new(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);

// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;

// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];

// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
// Set every third value to 255. A 24bpp bitmap will look red.
for (int counter = 2; counter < rgbValues.Length; counter += 3)
{
rgbValues[counter] = 255;
}

// Set every third value to 255. A 24bpp bitmap will look red.
for (int counter = 2; counter < rgbValues.Length; counter += 3)
rgbValues[counter] = 255;
// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);

// Unlock the bits.
bmp.UnlockBits(bmpData);
// Draw the modified image.
e.Graphics.DrawImage(bmp, 0, 150);
}

// Draw the modified image.
e.Graphics.DrawImage(bmp, 0, 150);
}

//</snippet5>

public static void Main()
public static void Run()
{
Application.Run(new Form1());
Application.Run(new BitmapConstructorForm1());
}
}
Loading
Loading