用C#简单写一个图片转换格式的小工具

效果图:
在这里插入图片描述
使用的 .NET Framework 4.8 创建
选择 WPF 应用(.NET Framework)
引用如下
在这里插入图片描述
MainWindow.xaml如下

<Window x:Class="Png2jpg.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:pu="clr-namespace:Panuon.UI.Silver;assembly=Panuon.UI.Silver"
        xmlns:local="clr-namespace:Png2jpg"
        mc:Ignorable="d"
        Title="img2any(请拖拽图片到下面空白处)" Height="139.165" Width="349.711" Icon="aefdh-2mbjq-001.ico" Topmost="True" DragEnter="Window_DragEnter" Drop="Window_Drop" AllowDrop="True" >
    <Grid VerticalAlignment="Top">
        <RadioButton x:Name="Rb2png" Content="ToPNG" HorizontalAlignment="Left" Margin="14,27,0,-9" VerticalAlignment="Top" pu:RadioButtonHelper.RadioButtonStyle="Switch2" IsChecked="True"/>
        <RadioButton x:Name="Rb2jpg" Content="ToJPG" HorizontalAlignment="Left" Margin="123,27,0,-9" VerticalAlignment="Top" pu:RadioButtonHelper.RadioButtonStyle="Switch2"/>
        <RadioButton x:Name="Rb2BMP" Content="ToBMP" HorizontalAlignment="Left" Margin="230,27,0,-9" VerticalAlignment="Top" pu:RadioButtonHelper.RadioButtonStyle="Switch2"/>
        <CheckBox x:Name="Cumname" Content="自定义文件名" HorizontalAlignment="Left" Margin="134,62,0,-22" VerticalAlignment="Top" FontSize="10" Height="18" FontWeight="Bold" Click="Cumname_Click"/>
        <TextBox x:Name="Cum_Textbox" HorizontalAlignment="Left" Height="18" Margin="215,62,0,-22" TextWrapping="Wrap" VerticalAlignment="Top" Width="101" FontSize="10" pu:TextBoxHelper.Watermark="勾选前面后修改此处" FontWeight="Bold" IsEnabled="False"/>
    </Grid>
</Window>

MainWindow.cs如下

using System.Windows;
using System.Drawing;
namespace Png2jpg
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effects = DragDropEffects.Copy;
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        }

        private void Window_Drop(object sender, DragEventArgs e)
        {
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            string FileSuffix = path.Substring((path.Length - 3), 3).ToLower();
            Title = path;
            if (FileSuffix == "png"|| FileSuffix == "bmp"|| FileSuffix == "jpg" || FileSuffix == "dds")
            {
                Image img = new Bitmap(path);
                if (Rb2png.IsChecked == true)
                {
                    if (Cum_Textbox.IsEnabled == true)
                    {
                        img.Save(Cum_Textbox.Text + ".png", System.Drawing.Imaging.ImageFormat.Png);
                    }
                    else
                    {
                        img.Save(FileSuffix + "2" + "png" + ".png", System.Drawing.Imaging.ImageFormat.Png);
                    }
                }
                else if (Rb2jpg.IsChecked == true)
                {
                    if (Cum_Textbox.IsEnabled == true)
                    {
                        img.Save(Cum_Textbox.Text + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                    else
                    {
                        img.Save(FileSuffix + "2" + "jpg" + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                }
                else if (Rb2BMP.IsChecked == true)
                {
                    if (Cum_Textbox.IsEnabled == true)
                    {
                        img.Save(Cum_Textbox.Text + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                    }
                    else
                    {
                        img.Save(FileSuffix + "2" + "bmp" + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                    }
                }
            }
            else
            {
                MessageBox.Show("文件格式错误,请拖入图片文件(png,bmp,jpg)");
            }
            
        }

        private void Cumname_Click(object sender, RoutedEventArgs e)
        {
            if (Cumname.IsChecked == true)
            {
                Cum_Textbox.IsEnabled = true;
            }
            else
            {
                Cum_Textbox.IsEnabled = false;
            }
        }
    }
}

程序展现图
在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值