Материал из .Net Framework эксперт
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Add Image to PictureBox
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class PictureBoxImageAdding : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
private PictureBox picBox = new PictureBox();
public PictureBoxImageAdding()
{
InitializeComponent();
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
picBox.Location = new System.Drawing.Point(64, 32);
picBox.Size = new System.Drawing.Size(50, 50);
picBox.Image = new Bitmap("yourFile.bmp");
Controls.Add(picBox);
}
protected override void Dispose( bool disposing )
{
picBox.Dispose();
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
}
[STAThread]
static void Main()
{
Application.Run(new PictureBoxImageAdding());
}
}
AutoSize PictureBox
using System;
using System.Drawing;
using System.Windows.Forms;
public class PictureBoxAutoSize : Form
{
public PictureBoxAutoSize()
{
Size = new Size(550,500);
AutoScroll = true;
Image img = Image.FromFile("YourFile.bmp");
Label lblAuto = new Label();
lblAuto.Parent = this;
lblAuto.Location = new Point(0, 250);
lblAuto.Size = new Size(75,25);
lblAuto.TextAlign = ContentAlignment.MiddleRight;
lblAuto.Text = "AutoSize:";
PictureBox pbAutoBig = new PictureBox();
pbAutoBig.Parent = this;
pbAutoBig.Size = new Size(200, 200);
pbAutoBig.Location = new Point(75, 250);
pbAutoBig.BorderStyle = BorderStyle.FixedSingle;
pbAutoBig.SizeMode = PictureBoxSizeMode.AutoSize;
pbAutoBig.Image = img;
}
static void Main()
{
Application.Run(new PictureBoxAutoSize());
}
}
CenterImage PictureBox
using System;
using System.Drawing;
using System.Windows.Forms;
public class PictureBoxCenterImage : Form
{
public PictureBoxCenterImage()
{
Size = new Size(550,500);
AutoScroll = true;
Image img = Image.FromFile("YourFile.bmp");
Label lblCenter = new Label();
lblCenter.Parent = this;
lblCenter.Location = new Point(0,480);
lblCenter.Size = new Size(75,25);
lblCenter.TextAlign = ContentAlignment.MiddleRight;
lblCenter.Text = "CenterImage:";
PictureBox pbCenterBig = new PictureBox();
pbCenterBig.Parent = this;
pbCenterBig.Size = new Size(200, 200);
pbCenterBig.Location = new Point(75,480);
pbCenterBig.BorderStyle = BorderStyle.FixedSingle;
pbCenterBig.SizeMode = PictureBoxSizeMode.CenterImage;
pbCenterBig.Image = img;
PictureBox pbCenterSmall = new PictureBox();
pbCenterSmall.Parent = this;
pbCenterSmall.Size = new Size(100, 100);
pbCenterSmall.Location = new Point(325,480);
pbCenterSmall.BorderStyle = BorderStyle.FixedSingle;
pbCenterSmall.SizeMode = PictureBoxSizeMode.CenterImage;
pbCenterSmall.Image = img;
}
static void Main()
{
Application.Run(new PictureBoxCenterImage());
}
}
Change PictureBox Cursor
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class PictureBoxCursorChange : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
private PictureBox picBox = new PictureBox();
public PictureBoxCursorChange()
{
InitializeComponent();
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
picBox.Location = new System.Drawing.Point(64, 32);
picBox.Size = new System.Drawing.Size(50, 50);
picBox.Image = new Bitmap("yourFile.bmp");
picBox.Cursor = Cursors.Hand;
Controls.Add(picBox);
}
protected override void Dispose( bool disposing )
{
picBox.Dispose();
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
}
[STAThread]
static void Main()
{
Application.Run(new PictureBoxCursorChange());
}
}
Create an in-memory bitmap and draw and Display the bitmap in the picture box
using System;
using System.Drawing;
using System.Windows.Forms;
public class BitmapCreateDrawing : Form
{
public BitmapCreateDrawing()
{
this.panel1 = new System.Windows.Forms.Panel();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel1.SuspendLayout();
((System.ruponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Size = new System.Drawing.Size(268, 242);
this.pictureBox1.Location = new System.Drawing.Point(73, 62);
this.pictureBox1.Size = new System.Drawing.Size(111, 144);
this.pictureBox1.TabStop = false;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(296, 270);
this.Controls.Add(this.panel1);
this.panel1.ResumeLayout(false);
((System.ruponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
string text = "string";
Font font = new Font("Tahoma", 20);
Bitmap b = new Bitmap(600, 600);
using (Graphics g = Graphics.FromImage(b))
{
g.FillRectangle(Brushes.White, new Rectangle(0, 0,
b.Width, b.Height));
g.DrawString(text, font, Brushes.Black, 50, 60);
}
pictureBox1.BackgroundImage = b;
pictureBox1.Size = b.Size;
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new BitmapCreateDrawing());
}
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.PictureBox pictureBox1;
}
Normal PictureBox
using System;
using System.Drawing;
using System.Windows.Forms;
public class PictureBoxNormal : Form
{
public PictureBoxNormal()
{
Size = new Size(550,500);
AutoScroll = true;
Image img = Image.FromFile("YourFile.bmp");
Label lblNormal = new Label();
lblNormal.Parent = this;
lblNormal.Location = new Point(0,20);
lblNormal.Size = new Size(75,25);
lblNormal.TextAlign = ContentAlignment.MiddleRight;
lblNormal.Text = "Normal:";
PictureBox pbNormalBig = new PictureBox();
pbNormalBig.Parent = this;
pbNormalBig.Size = new Size(200, 200);
pbNormalBig.Location = new Point(75,20);
pbNormalBig.BorderStyle = BorderStyle.FixedSingle;
pbNormalBig.SizeMode = PictureBoxSizeMode.Normal;
pbNormalBig.Image = img;
}
static void Main()
{
Application.Run(new PictureBoxNormal());
}
}
StretchImage PictureBox
using System;
using System.Drawing;
using System.Windows.Forms;
public class PictureBoxStretchImage : Form
{
public PictureBoxStretchImage()
{
Size = new Size(550,500);
AutoScroll = true;
Image img = Image.FromFile("YourFile.bmp");
Label lblStretch = new Label();
lblStretch.Parent = this;
lblStretch.Location = new Point(0,710);
lblStretch.Size = new Size(75,25);
lblStretch.TextAlign = ContentAlignment.MiddleRight;
lblStretch.Text = "StretchImage:";
PictureBox pbStretchBig = new PictureBox();
pbStretchBig.Parent = this;
pbStretchBig.Size = new Size(200, 200);
pbStretchBig.Location = new Point(75,710);
pbStretchBig.BorderStyle = BorderStyle.FixedSingle;
pbStretchBig.SizeMode = PictureBoxSizeMode.StretchImage;
pbStretchBig.Image = img;
PictureBox pbStretchSmall = new PictureBox();
pbStretchSmall.Parent = this;
pbStretchSmall.Size = new Size(100, 100);
pbStretchSmall.Location = new Point(325,710);
pbStretchSmall.BorderStyle = BorderStyle.FixedSingle;
pbStretchSmall.SizeMode = PictureBoxSizeMode.StretchImage;
pbStretchSmall.Image = img;
}
static void Main()
{
Application.Run(new PictureBoxStretchImage());
}
}