C#实现图片缩略图功能的示例详解

.NET
252
0
0
2023-07-09
标签   C#
目录
  • 实践过程
  • 效果
  • 代码

实践过程

效果

代码

public partial class Form : Form
{
    public Form()
    {
        InitializeComponent();
    }

    public Image ResourceImage;
    private int ImageWidth;
    private int ImageHeight;
    public string ErrMessage;

    public bool ThumbnailCallback()
    {
        return false;
    }

    public bool GetReducedImage(double Percent, string targetFilePath)
    {
        try
        {
            Bitmap bt = new Bitmap(, 120);
            Graphics g = Graphics.FromImage(bt);
            g.Clear(Color.White);
            Image ReducedImage;
            Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            ImageWidth = Convert.ToInt(ResourceImage.Width * Percent);
            ImageHeight = Convert.ToInt(ResourceImage.Height * Percent);
            ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
            if (ImageWidth > ImageHeight)
            {
                g.DrawImage(ReducedImage,, (int) (120 - ImageHeight) / 2, ImageWidth, ImageHeight);
            }
            else
            {
                g.DrawImage(ReducedImage, (int) ( - ImageWidth) / 2, 0, ImageWidth, ImageHeight);
            }

            g.DrawRectangle(new Pen(Color.Gainsboro),, 0, 119, 119);
            bt.Save(@targetFilePath, ImageFormat.Jpeg);
            bt.Dispose();
            ReducedImage.Dispose();
            return true;
        }
        catch (Exception e)
        {
            ErrMessage = e.Message;
            return false;
        }
    }

    private void button_Click(object sender, EventArgs e)
    {
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            pictureBox.Image = Image.FromFile(openFileDialog1.FileName);
        }
    }

    private void button_Click(object sender, EventArgs e)
    {
        double percent;
        string imgpath = openFileDialog.FileName;
        string imgName = imgpath.ToString().Substring(imgpath.ToString().LastIndexOf("\\") +,
            imgpath.ToString().Length - - imgpath.ToString().LastIndexOf("\\"));
        imgName = imgName.Remove(imgName.LastIndexOf("."));
        if (openFileDialog.FileName.Length != 0)
        {
            ResourceImage = Image.FromFile(openFileDialog.FileName);
            if (ResourceImage.Width < ResourceImage.Height)
            {
                percent = (double) / ResourceImage.Height;
            }
            else
            {
                percent = (double) / ResourceImage.Width;
            }

            GetReducedImage(percent, "c:\\_" + imgName + ".JPG");
            pictureBox.Image = Image.FromFile("c:\\_" + imgName + ".JPG");
        }
    }
}
partial class Form
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.pictureBox = new System.Windows.Forms.PictureBox();
        this.pictureBox = new System.Windows.Forms.PictureBox();
        this.button = new System.Windows.Forms.Button();
        this.button = new System.Windows.Forms.Button();
        this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
        this.SuspendLayout();
        // 
        // pictureBox
        // 
        this.pictureBox.Location = new System.Drawing.Point(11, 42);
        this.pictureBox.Name = "pictureBox1";
        this.pictureBox.Size = new System.Drawing.Size(262, 232);
        this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
        this.pictureBox.TabIndex = 0;
        this.pictureBox.TabStop = false;
        // 
        // pictureBox
        // 
        this.pictureBox.Location = new System.Drawing.Point(305, 80);
        this.pictureBox.Name = "pictureBox2";
        this.pictureBox.Size = new System.Drawing.Size(155, 132);
        this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
        this.pictureBox.TabIndex = 1;
        this.pictureBox.TabStop = false;
        // 
        // button
        // 
        this.button.Location = new System.Drawing.Point(11, 13);
        this.button.Name = "button1";
        this.button.Size = new System.Drawing.Size(75, 23);
        this.button.TabIndex = 2;
        this.button.Text = "选择图片";
        this.button.UseVisualStyleBackColor = true;
        this.button.Click += new System.EventHandler(this.button1_Click);
        // 
        // button
        // 
        this.button.Location = new System.Drawing.Point(93, 12);
        this.button.Name = "button2";
        this.button.Size = new System.Drawing.Size(75, 23);
        this.button.TabIndex = 3;
        this.button.Text = "显示缩略图";
        this.button.UseVisualStyleBackColor = true;
        this.button.Click += new System.EventHandler(this.button2_Click);
        // 
        // openFileDialog
        // 
        this.openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.bmp;*.png";
        // 
        // Form
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(, 296);
        this.Controls.Add(this.button);
        this.Controls.Add(this.button);
        this.Controls.Add(this.pictureBox);
        this.Controls.Add(this.pictureBox);
        this.Name = "Form";
        this.Text = "Form";
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.PictureBox pictureBox;
    private System.Windows.Forms.PictureBox pictureBox;
    private System.Windows.Forms.Button button;
    private System.Windows.Forms.Button button;
    private System.Windows.Forms.OpenFileDialog openFileDialog;
}