C#*.bmp,*.jpg,*.png指定颜色,转换为透明背景的png

2 篇文章 0 订阅

透明背景

直接上代码

partial class Form1
{
///
///
private System.ComponentModel.IContainer components = null;

  protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 

    /// <summary>
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        this.Image = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.imgPath = new System.Windows.Forms.TextBox();
        this.saveImagePath = new System.Windows.Forms.TextBox();
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
        this.listView1 = new System.Windows.Forms.ListView();
        this.imageList1 = new System.Windows.Forms.ImageList(this.components);
        this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
        this.listView2 = new System.Windows.Forms.ListView();
        this.LargeIcon = new System.Windows.Forms.RadioButton();
        this.SmallIcon = new System.Windows.Forms.RadioButton();
        this.IconList = new System.Windows.Forms.RadioButton();
        this.radioButton1 = new System.Windows.Forms.RadioButton();
        this.radioButton2 = new System.Windows.Forms.RadioButton();
        this.radioButton3 = new System.Windows.Forms.RadioButton();
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.label1 = new System.Windows.Forms.Label();
        this.label3 = new System.Windows.Forms.Label();
        this.imageList2 = new System.Windows.Forms.ImageList(this.components);
        this.label4 = new System.Windows.Forms.Label();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.groupBox2 = new System.Windows.Forms.GroupBox();
        this.pictureBox2 = new System.Windows.Forms.PictureBox();
        this.groupBox3 = new System.Windows.Forms.GroupBox();
        this.label5 = new System.Windows.Forms.Label();
        this.button3 = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.groupBox1.SuspendLayout();
        this.groupBox2.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
        this.groupBox3.SuspendLayout();
        this.SuspendLayout();
        // 
        // Image
        // 
        this.Image.AutoSize = true;
        this.Image.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
        this.Image.Location = new System.Drawing.Point(22, 25);
        this.Image.Name = "Image";
        this.Image.Size = new System.Drawing.Size(90, 12);
        this.Image.TabIndex = 0;
        this.Image.Text = "读取画像路径:";
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
        this.label2.Location = new System.Drawing.Point(22, 54);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(90, 12);
        this.label2.TabIndex = 1;
        this.label2.Text = "保存画像路径:";
        // 
        // imgPath
        // 
        this.imgPath.Location = new System.Drawing.Point(121, 23);
        this.imgPath.Name = "imgPath";
        this.imgPath.Size = new System.Drawing.Size(367, 19);
        this.imgPath.TabIndex = 2;
        this.imgPath.Text = "F:\\12_Schema_Test\\【GX】STAMP";
        // 
        // saveImagePath
        // 
        this.saveImagePath.Location = new System.Drawing.Point(121, 51);
        this.saveImagePath.Name = "saveImagePath";
        this.saveImagePath.Size = new System.Drawing.Size(367, 19);
        this.saveImagePath.TabIndex = 3;
        this.saveImagePath.Text = "F:\\12_Schema_Test\\【GX】STAMP_PNG";
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(512, 17);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 25);
        this.button1.TabIndex = 5;
        this.button1.Text = "读取";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(512, 48);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(75, 25);
        this.button2.TabIndex = 6;
        this.button2.Text = "变换";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // openFileDialog1
        // 
        this.openFileDialog1.FileName = "openFileDialog1";
        // 
        // listView1
        // 
        this.listView1.Location = new System.Drawing.Point(24, 155);
        this.listView1.Name = "listView1";
        this.listView1.Size = new System.Drawing.Size(335, 339);
        this.listView1.TabIndex = 7;
        this.listView1.UseCompatibleStateImageBehavior = false;
        this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
        // 
        // imageList1
        // 
        this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
        this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
        this.imageList1.Images.SetKeyName(0, "TOOL.JPG");
        // 
        // listView2
        // 
        this.listView2.Location = new System.Drawing.Point(445, 155);
        this.listView2.Name = "listView2";
        this.listView2.Size = new System.Drawing.Size(339, 339);
        this.listView2.TabIndex = 8;
        this.listView2.UseCompatibleStateImageBehavior = false;
        // 
        // LargeIcon
        // 
        this.LargeIcon.AutoSize = true;
        this.LargeIcon.Checked = true;
        this.LargeIcon.Location = new System.Drawing.Point(5, 18);
        this.LargeIcon.Name = "LargeIcon";
        this.LargeIcon.Size = new System.Drawing.Size(62, 16);
        this.LargeIcon.TabIndex = 9;
        this.LargeIcon.TabStop = true;
        this.LargeIcon.Text = "大图标";
        this.LargeIcon.UseVisualStyleBackColor = true;
        this.LargeIcon.CheckedChanged += new System.EventHandler(this.LargeIcon_CheckedChanged);
        // 
        // SmallIcon
        // 
        this.SmallIcon.AutoSize = true;
        this.SmallIcon.Location = new System.Drawing.Point(93, 18);
        this.SmallIcon.Name = "SmallIcon";
        this.SmallIcon.Size = new System.Drawing.Size(62, 16);
        this.SmallIcon.TabIndex = 10;
        this.SmallIcon.TabStop = true;
        this.SmallIcon.Text = "小图标";
        this.SmallIcon.UseVisualStyleBackColor = true;
        this.SmallIcon.CheckedChanged += new System.EventHandler(this.SmallIcon_CheckedChanged);
        // 
        // IconList
        // 
        this.IconList.AutoSize = true;
        this.IconList.Location = new System.Drawing.Point(183, 18);
        this.IconList.Name = "IconList";
        this.IconList.Size = new System.Drawing.Size(49, 16);
        this.IconList.TabIndex = 11;
        this.IconList.TabStop = true;
        this.IconList.Text = "列表";
        this.IconList.UseVisualStyleBackColor = true;
        this.IconList.CheckedChanged += new System.EventHandler(this.IconList_CheckedChanged);
        // 
        // radioButton1
        // 
        this.radioButton1.AutoSize = true;
        this.radioButton1.Checked = true;
        this.radioButton1.Location = new System.Drawing.Point(10, 18);
        this.radioButton1.Name = "radioButton1";
        this.radioButton1.Size = new System.Drawing.Size(57, 16);
        this.radioButton1.TabIndex = 12;
        this.radioButton1.TabStop = true;
        this.radioButton1.Text = "*.bmp";
        this.radioButton1.UseVisualStyleBackColor = true;
        // 
        // radioButton2
        // 
        this.radioButton2.AutoSize = true;
        this.radioButton2.Location = new System.Drawing.Point(73, 18);
        this.radioButton2.Name = "radioButton2";
        this.radioButton2.Size = new System.Drawing.Size(54, 16);
        this.radioButton2.TabIndex = 13;
        this.radioButton2.TabStop = true;
        this.radioButton2.Text = "*.png";
        this.radioButton2.UseVisualStyleBackColor = true;
        // 
        // radioButton3
        // 
        this.radioButton3.AutoSize = true;
        this.radioButton3.Location = new System.Drawing.Point(137, 18);
        this.radioButton3.Name = "radioButton3";
        this.radioButton3.Size = new System.Drawing.Size(51, 16);
        this.radioButton3.TabIndex = 14;
        this.radioButton3.TabStop = true;
        this.radioButton3.Text = "*.jpg";
        this.radioButton3.UseVisualStyleBackColor = true;
        // 
        // pictureBox1
        // 
        this.pictureBox1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.pictureBox1.Location = new System.Drawing.Point(24, 524);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(198, 112);
        this.pictureBox1.TabIndex = 15;
        this.pictureBox1.TabStop = false;
        this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.ForeColor = System.Drawing.Color.Crimson;
        this.label1.Location = new System.Drawing.Point(248, 533);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(95, 12);
        this.label1.TabIndex = 17;
        this.label1.Text = "要替换的背景色:";
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.label3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.label3.Location = new System.Drawing.Point(268, 556);
        this.label3.MinimumSize = new System.Drawing.Size(50, 30);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(50, 30);
        this.label3.TabIndex = 18;
        // 
        // imageList2
        // 
        this.imageList2.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
        this.imageList2.ImageSize = new System.Drawing.Size(16, 16);
        this.imageList2.TransparentColor = System.Drawing.Color.Transparent;
        // 
        // label4
        // 
        this.label4.AutoSize = true;
        this.label4.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
        this.label4.ForeColor = System.Drawing.SystemColors.HotTrack;
        this.label4.Location = new System.Drawing.Point(24, 509);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(143, 12);
        this.label4.TabIndex = 19;
        this.label4.Text = "请选择要转换的背景颜色:";
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.Add(this.SmallIcon);
        this.groupBox1.Controls.Add(this.LargeIcon);
        this.groupBox1.Controls.Add(this.IconList);
        this.groupBox1.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
        this.groupBox1.ForeColor = System.Drawing.Color.DarkCyan;
        this.groupBox1.Location = new System.Drawing.Point(24, 91);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(249, 45);
        this.groupBox1.TabIndex = 20;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "读入画像的表示方式";
        // 
        // groupBox2
        // 
        this.groupBox2.Controls.Add(this.radioButton2);
        this.groupBox2.Controls.Add(this.radioButton1);
        this.groupBox2.Controls.Add(this.radioButton3);
        this.groupBox2.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
        this.groupBox2.ForeColor = System.Drawing.Color.DarkCyan;
        this.groupBox2.Location = new System.Drawing.Point(288, 91);
        this.groupBox2.Name = "groupBox2";
        this.groupBox2.Size = new System.Drawing.Size(200, 45);
        this.groupBox2.TabIndex = 21;
        this.groupBox2.TabStop = false;
        this.groupBox2.Text = "读取文件类型";
        // 
        // pictureBox2
        // 
        this.pictureBox2.BackgroundImage = global::SchemaStampImageTransformation.Properties.Resources.Right;
        this.pictureBox2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
        this.pictureBox2.Location = new System.Drawing.Point(365, 294);
        this.pictureBox2.Margin = new System.Windows.Forms.Padding(0);
        this.pictureBox2.Name = "pictureBox2";
        this.pictureBox2.Size = new System.Drawing.Size(74, 38);
        this.pictureBox2.TabIndex = 22;
        this.pictureBox2.TabStop = false;
        this.pictureBox2.Click += new System.EventHandler(this.button2_Click);
        // 
        // groupBox3
        // 
        this.groupBox3.Controls.Add(this.label5);
        this.groupBox3.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
        this.groupBox3.ForeColor = System.Drawing.Color.DarkCyan;
        this.groupBox3.Location = new System.Drawing.Point(519, 91);
        this.groupBox3.Name = "groupBox3";
        this.groupBox3.Size = new System.Drawing.Size(144, 45);
        this.groupBox3.TabIndex = 23;
        this.groupBox3.TabStop = false;
        this.groupBox3.Text = "转换后文件类型:";
        // 
        // label5
        // 
        this.label5.AutoSize = true;
        this.label5.Font = new System.Drawing.Font("MS UI Gothic", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
        this.label5.ForeColor = System.Drawing.Color.Crimson;
        this.label5.Location = new System.Drawing.Point(40, 17);
        this.label5.Name = "label5";
        this.label5.Size = new System.Drawing.Size(45, 15);
        this.label5.TabIndex = 0;
        this.label5.Text = "*.png";
        // 
        // button3
        // 
        this.button3.Location = new System.Drawing.Point(709, 126);
        this.button3.Name = "button3";
        this.button3.Size = new System.Drawing.Size(75, 23);
        this.button3.TabIndex = 24;
        this.button3.Text = "打开文件夹";
        this.button3.UseVisualStyleBackColor = true;
        this.button3.Click += new System.EventHandler(this.button3_Click_1);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(841, 648);
        this.Controls.Add(this.button3);
        this.Controls.Add(this.groupBox3);
        this.Controls.Add(this.pictureBox2);
        this.Controls.Add(this.groupBox2);
        this.Controls.Add(this.groupBox1);
        this.Controls.Add(this.label4);
        this.Controls.Add(this.label3);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.pictureBox1);
        this.Controls.Add(this.listView2);
        this.Controls.Add(this.listView1);
        this.Controls.Add(this.button2);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.saveImagePath);
        this.Controls.Add(this.imgPath);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.Image);
        this.Name = "Form1";
        this.Text = "画像转换工具_v1.1.01测试版";
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.groupBox2.ResumeLayout(false);
        this.groupBox2.PerformLayout();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
        this.groupBox3.ResumeLayout(false);
        this.groupBox3.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Label Image;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox imgPath;
    private System.Windows.Forms.TextBox saveImagePath;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.OpenFileDialog openFileDialog1;
    private System.Windows.Forms.ListView listView1;
    private System.Windows.Forms.ImageList imageList1;
    private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
    private System.Windows.Forms.ListView listView2;
    private System.Windows.Forms.RadioButton LargeIcon;
    private System.Windows.Forms.RadioButton SmallIcon;
    private System.Windows.Forms.RadioButton IconList;
    private System.Windows.Forms.RadioButton radioButton1;
    private System.Windows.Forms.RadioButton radioButton2;
    private System.Windows.Forms.RadioButton radioButton3;
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.ImageList imageList2;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.PictureBox pictureBox2;
    private System.Windows.Forms.GroupBox groupBox3;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.Button button3;
}

}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SchemaStampImageTransformation
{
public partial class Form1 : Form
{

    public string savePath = string.Empty;
    public string imagePath = string.Empty;

    public FileInfo[] Images;

    public Form1()
    {
        InitializeComponent();
        this.listView1.View = View.LargeIcon;

    }

    private void button1_Click(object sender, EventArgs e)
    {
        imagePath = this.imgPath.Text;

        if (string.IsNullOrEmpty(imagePath.Trim()))
        {
            this.folderBrowserDialog1.ShowDialog();
            if (this.folderBrowserDialog1.SelectedPath == "")
            {
                MessageBox.Show("请选择一个路径!");
                return;
            }
            else
            {
                imagePath = this.folderBrowserDialog1.SelectedPath;
            }
        }

        imagePath += "\\";

        SetListViewItemsRead(listView1, this.imageList1, GetGroupBoxValue(this.groupBox2), imagePath);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        List<string> resultImageNameList = new List<string>();

        savePath = this.saveImagePath.Text.Trim();
        if (string.IsNullOrWhiteSpace(savePath))
        {
            this.folderBrowserDialog1.ShowDialog();
            if (this.folderBrowserDialog1.SelectedPath == "")
            {
                MessageBox.Show("请选择保存图片的路径!");
                return;
            }
            else
            {
                this.saveImagePath.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }

        savePath = this.saveImagePath.Text.Trim();
        savePath += "\\";

        int selectedItemsCount = this.listView1.SelectedItems.Count;
        if (selectedItemsCount == 0)
        {
            MessageBox.Show("请选择要转换的图片!");
            return;
        }
        else
        {
            Bitmap bmp;
            string switchFileName = string.Empty;
            string existsFileName = string.Empty;

            for (int i = 0; i < selectedItemsCount; i++)
            {
                bmp = new Bitmap(this.listView1.SelectedItems[i].Tag.ToString());
                if (bmp != null)
                {
                    switchFileName = this.listView1.SelectedItems[i].Text.Replace(".BMP", ".png").Replace(".bmp", ".png").Replace(".jpg", ".png").Replace(".jpeg", ".png").Replace(".PNG", ".png");
                    existsFileName += savePath + switchFileName;
                    if (System.IO.File.Exists(existsFileName))
                    {
                        if (MessageBox.Show("目标文【" + switchFileName + "】件已存在,", "请确认是否覆盖原图!",
                            MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
                        {
                            System.IO.File.Delete(existsFileName);
                        }
                        else
                        {
                            break;
                        }
                    }

                    resultImageNameList.Add(switchFileName);
                    SaveBmpAsPNG(bmp, savePath, this.listView1.SelectedItems[i].Text.Replace(".BMP", "")
                        .Replace(".bmp", "").Replace(".jpg", "").Replace(".JPG", "").Replace(".jpeg", "").Replace(".png", "").Replace(".PNG", ""));
                }
            }
        }

        SetListViewItems(listView2, this.imageList2, this.label5.Text, savePath, resultImageNameList);
        MessageBox.Show("图片格式转换完成!");

    }

    /// <summary>
    /// 指定的颜色背静透明并保存为png
    /// </summary>
    /// <param name="bmp"></param>
    /// <param name="savePath"></param>
    /// <param name="fileName"></param>
    private void SaveBmpAsPNG(Bitmap bmp, string savePath,string fileName)
    {

        string savePngPath = savePath + fileName + ".png";
        savePngPath.Replace(".bmp", "").Replace(".BMP","");

        // 获取指定的颜色
        Color seltctedColor = this.label3.BackColor;

        // 指定的颜色背静透明
        if (seltctedColor != System.Drawing.SystemColors.ActiveCaption)
        {
            bmp.MakeTransparent(Color.FromArgb(0, seltctedColor));
            bmp.MakeTransparent(Color.FromArgb(0, Color.White));
        }
      
        bmp.Save(savePngPath, ImageFormat.Png);
    }

    private void LargeIcon_CheckedChanged(object sender, EventArgs e)
    {
        this.listView1.View = View.LargeIcon;
    }

    private void SmallIcon_CheckedChanged(object sender, EventArgs e)
    {
        this.listView1.View = View.SmallIcon;
    }

    private void IconList_CheckedChanged(object sender, EventArgs e)
    {
        this.listView1.View = View.List;
    }

    private void Details_CheckedChanged(object sender, EventArgs e)
    {
        this.listView1.View = View.Details;
    }

    private void button3_Click(object sender, EventArgs e)
    {

    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        Bitmap btp = pictureBox1.Image as Bitmap;

        try
        {
            Color c = btp.GetPixel(e.X, e.Y);

            label3.BackColor = c;
        }
        catch (Exception)
        {
            MessageBox.Show("请在图片区域内选择颜色!");
        }
    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int selectedItemsCount = this.listView1.SelectedItems.Count;
        if (selectedItemsCount == 0)
        {
            return;
        }
        else
        {
            this.pictureBox1.Image = System.Drawing.Image.FromFile(this.listView1.SelectedItems[selectedItemsCount - 1].Tag.ToString());
        }
    }

    private void button4_Click(object sender, EventArgs e)
    {
        this.pictureBox1.Image = null;
        this.label3.BackColor = System.Drawing.SystemColors.ActiveCaption;
    }


    private void SetListViewItemsRead(ListView listView, ImageList imageList, string getImageType, string imagePath)
    {
        DirectoryInfo Folder;

        if (string.IsNullOrWhiteSpace(imagePath))
        {
            MessageBox.Show("请输入正确的图片路径!");
        }

        // 转换后的图片预览设定
        listView.View = View.LargeIcon;

        Folder = new DirectoryInfo(imagePath);
        Images = Folder.GetFiles(getImageType);

        if (Images != null && Images.Length > 0)
        {
            imageList.Images.Clear();
            listView.Items.Clear();

            // 读取图片时预览设定
            try
            {
                foreach (var item in Images)
                {
                    imageList.Images.Add(System.Drawing.Image.FromFile(imagePath + item.Name));
                }
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("对象文件不存在!");
            }
            listView.SmallImageList = imageList;
            listView.LargeImageList = imageList;

            for (int i = 0; i < Images.Length; i++)
            {
                ListViewItem listViewItem = new ListViewItem();

                listViewItem.ImageIndex = i;
                listViewItem.Text = Images[i].Name;
                listViewItem.Tag = imagePath + Images[i].Name;
                listView.Items.Add(listViewItem);
            }
        }
        else
        {
            MessageBox.Show("没有满足条件的文件!");
        }
    }

    private void SetListViewItems(ListView listView, ImageList imageList, string getImageType, string imagePath, 
        List<string> resultImageNameList)
    {
        DirectoryInfo Folder;

        if (string.IsNullOrWhiteSpace(imagePath))
        {
            MessageBox.Show("请输入正确的图片路径!");
        }

        Folder = new DirectoryInfo(imagePath);
        Images = Folder.GetFiles(getImageType);
        
        if (Images != null && Images.Length > 0)
        {
            imageList.Images.Clear();
            listView.Items.Clear();

            // 转换后的图片预览设定
            listView.View = View.LargeIcon;

            foreach (var item in resultImageNameList)
            {
                imageList.Images.Add(System.Drawing.Image.FromFile(imagePath + item));
            }

            listView.SmallImageList = imageList;
            listView.LargeImageList = imageList;

            for (int i = 0; i < resultImageNameList.Count; i++)
            {
                ListViewItem listViewItem = new ListViewItem();

                listViewItem.ImageIndex = i;
                listViewItem.Text = resultImageNameList[i];
                listViewItem.Tag = imagePath + resultImageNameList[i];
                listView.Items.Add(listViewItem);
            }
        }
        else
        {
            MessageBox.Show("没有满足条件的文件!");
        }
    }

    /// <summary>
    /// 获取GroupBox中选中的内容
    /// </summary>
    /// <param name="groupBox"></param>
    /// <returns></returns>
    private string GetGroupBoxValue(GroupBox groupBox)
    {
        string result = string.Empty;

        if (groupBox != null)
        {
            for (int i = 0; i < groupBox.Controls.Count; i++)
            {
                RadioButton temp = groupBox.Controls[i] as RadioButton;
                if (temp != null && temp.Checked)
                {
                    result = temp.Text;
                }
            }
        }

        return result;
    }

    private void button3_Click_1(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start(this.saveImagePath.Text); 
    }

    private void pictureBox2_Click(object sender, EventArgs e)
    {

    }
}

}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值