Csharp/CSharp Tutorial/2D/Draw Image

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Resize a bitmap in drawing

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
public class DoubleBuffering : Form
{
    public DoubleBuffering()
    {
        InitializeComponent();
        image = Image.FromFile("YourFile.bmp");
        tmrRefresh.Start();
    }
    private int imageSize = 0;
    private Image image;
    private void tmrRefresh_Tick(object sender, EventArgs e)
    {
        imageSize++;
        this.Invalidate();
    }
    private void DoubleBuffering_Paint(object sender, PaintEventArgs e)
    {
        Graphics g;
        g = e.Graphics;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.FillRectangle(Brushes.Yellow, new Rectangle(new Point(0, 0),this.ClientSize));
        g.DrawImage(image, 50, 50, 50 + imageSize, 50 + imageSize);
    }
    private void chkUseDoubleBuffering_CheckedChanged(object sender, EventArgs e)
    {
        this.DoubleBuffered = chkUseDoubleBuffering.Checked;
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new DoubleBuffering());
    }
    private System.ruponentModel.IContainer components = null;
    private void InitializeComponent()
    {
        this.ruponents = new System.ruponentModel.Container();
        this.tmrRefresh = new System.Windows.Forms.Timer(this.ruponents);
        this.chkUseDoubleBuffering = new System.Windows.Forms.CheckBox();
        this.SuspendLayout();
        // 
        // tmrRefresh
        // 
        this.tmrRefresh.Tick += new System.EventHandler(this.tmrRefresh_Tick);
        // 
        // chkUseDoubleBuffering
        // 
        this.chkUseDoubleBuffering.AutoSize = true;
        this.chkUseDoubleBuffering.Location = new System.Drawing.Point(12, 3);
        this.chkUseDoubleBuffering.Size = new System.Drawing.Size(127, 17);
        this.chkUseDoubleBuffering.TabIndex = 0;
        this.chkUseDoubleBuffering.Text = "Use Double Buffering";
        this.chkUseDoubleBuffering.UseVisualStyleBackColor = true;
        this.chkUseDoubleBuffering.CheckedChanged += new System.EventHandler(this.chkUseDoubleBuffering_CheckedChanged);
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(299, 277);
        this.Controls.Add(this.chkUseDoubleBuffering);
        this.Text = "Double Buffering";
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.DoubleBuffering_Paint);
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.Timer tmrRefresh;
    private System.Windows.Forms.CheckBox chkUseDoubleBuffering;
}

Thumbnail Image Creation

using System;
using System.Drawing;
using System.Windows.Forms;
public class ThumbnailImageCreation : Form
{
    public ThumbnailImageCreation()
    {
        this.SuspendLayout();
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(299, 273);
        this.Text = "Thumbnails";
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.ThumbnailImageCreation_Paint);
        this.Load += new System.EventHandler(this.ThumbnailImageCreation_Load);
        this.ResumeLayout(false);
    }
    Image thumbnail;
    private void ThumbnailImageCreation_Load(object sender, EventArgs e)
    {
        Image img = Image.FromFile("YourFile.jpg");
        
        int thumbnailWidth = 200, thumbnailHeight = 100;
        thumbnail = img.GetThumbnailImage(thumbnailWidth, thumbnailHeight,
          null, IntPtr.Zero);
        
    }
    private void ThumbnailImageCreation_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImage(thumbnail, 10, 10);
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new ThumbnailImageCreation());
    }
}