效果图:
使用的 .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;
}
}
}
}
程序展现图