176 lines
5.2 KiB
C#
176 lines
5.2 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Input;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.Media.Imaging;
|
|
using Avalonia.Platform.Storage;
|
|
using MES.Utility.Core;
|
|
using SkiaSharp;
|
|
using Ursa.Controls;
|
|
using ZXing;
|
|
using 常用工具集.Base;
|
|
using 常用工具集.Utility.Qrcode;
|
|
|
|
namespace 常用工具集;
|
|
|
|
public partial class 二维码条形码生成 : UserControl
|
|
{
|
|
private SKBitmap Image;
|
|
private string svg;
|
|
public 二维码条形码生成()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private Bitmap BitmapToBitmapImage(SKBitmap skBitmap)
|
|
{
|
|
byte[] bytes = null;
|
|
using (MemoryStream stream = new MemoryStream())
|
|
{
|
|
skBitmap.Encode(SKEncodedImageFormat.Png, 100).SaveTo(stream);
|
|
bytes = stream.ToArray();
|
|
}
|
|
using (MemoryStream stream = new MemoryStream(bytes))
|
|
{
|
|
return new Bitmap(stream);
|
|
}
|
|
}
|
|
|
|
private void btnGenerate_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (txtContent.Text.IsNullOrEmpty())
|
|
{
|
|
MessageBox.ShowAsync("请输入内容");
|
|
return;
|
|
}
|
|
if (rdChecked1.IsChecked.Value)
|
|
{
|
|
SKBitmap image = QRCodeUtils.EncodeQrCode(txtContent.Text, 900, out svg);
|
|
Image = image;
|
|
image1.Source = BitmapToBitmapImage(image);
|
|
}
|
|
else if (rdChecked2.IsChecked.Value)
|
|
{
|
|
SKBitmap image = DataMatrixUtils.EncodeDataMatrix(DmtxSymbolSize.DmtxSymbol12x12, txtContent.Text, 900, out svg);
|
|
Image = image;
|
|
image1.Source = BitmapToBitmapImage(image);
|
|
}
|
|
else if (rdChecked3.IsChecked.Value)
|
|
{
|
|
SKBitmap image = DataMatrixUtils.EncodeDataMatrix(DmtxSymbolSize.DmtxSymbol16x16, txtContent.Text, 900, out svg);
|
|
Image = image;
|
|
image1.Source = BitmapToBitmapImage(image);
|
|
}
|
|
else if (rdChecked4.IsChecked.Value)
|
|
{
|
|
SKBitmap image = BarCodeUtils.EncodeBarCode(BarcodeFormat.CODE_39, txtContent.Text, 900, 500, out svg);
|
|
Image = image;
|
|
image1.Source = BitmapToBitmapImage(image);
|
|
}
|
|
else if (rdChecked5.IsChecked.Value)
|
|
{
|
|
SKBitmap image = BarCodeUtils.EncodeBarCode(BarcodeFormat.CODE_128, txtContent.Text, 900, 500, out svg);
|
|
Image = image;
|
|
image1.Source = BitmapToBitmapImage(image);
|
|
}
|
|
}
|
|
private async void btnSave_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (Image == null)
|
|
return;
|
|
|
|
var sp = GlobalValues.StorageProvider;
|
|
if (sp is null) return;
|
|
var result = await sp.SaveFilePickerAsync(new FilePickerSaveOptions()
|
|
{
|
|
Title = "保存图像",
|
|
DefaultExtension = "png"
|
|
});
|
|
if (result == null) return;
|
|
string temp = result.Path.LocalPath;
|
|
Save(temp, Image);
|
|
}
|
|
|
|
private void Save(string path, SKBitmap bitmap)
|
|
{
|
|
using (var stream = File.OpenWrite(path))
|
|
{
|
|
bitmap.Encode(SKEncodedImageFormat.Png, 100).SaveTo(stream);
|
|
}
|
|
}
|
|
private async void btnSaveSVG_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (Image == null)
|
|
return;
|
|
var sp = GlobalValues.StorageProvider;
|
|
if (sp is null) return;
|
|
var result = await sp.SaveFilePickerAsync(new FilePickerSaveOptions()
|
|
{
|
|
Title = "保存SVG",
|
|
DefaultExtension = "svg"
|
|
});
|
|
if (result == null) return;
|
|
string temp = result.Path.LocalPath;
|
|
System.IO.File.WriteAllText(temp, svg, Encoding.UTF8);
|
|
}
|
|
|
|
private void TextBox_KeyDown(object? sender, Avalonia.Input.KeyEventArgs e)
|
|
{
|
|
if (e.KeyModifiers == Avalonia.Input.KeyModifiers.Control)
|
|
{
|
|
e.Handled = true;
|
|
if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (e.Key == Key.V)
|
|
{
|
|
|
|
}
|
|
return;
|
|
}
|
|
if (e.Key == Key.Enter)
|
|
{
|
|
btnGenerate_Click(sender, e);
|
|
}
|
|
else if (e.KeyModifiers == KeyModifiers.Control && e.Key == Key.S)
|
|
{
|
|
e.Handled = true;
|
|
btnSave_Click(sender, e);
|
|
}
|
|
}
|
|
|
|
|
|
private void UserControl_KeyDown(object? sender, Avalonia.Input.KeyEventArgs e)
|
|
{
|
|
if (e.Key == Key.Enter)
|
|
{
|
|
btnGenerate_Click(sender, e);
|
|
}
|
|
else if (e.KeyModifiers == KeyModifiers.Control && e.Key == Key.C)
|
|
{
|
|
e.Handled = true;
|
|
if (Image == null)
|
|
return;
|
|
byte[] bytes = null;
|
|
using (MemoryStream stream = new MemoryStream())
|
|
{
|
|
Image.Encode(SKEncodedImageFormat.Png, 100).SaveTo(stream);
|
|
bytes = stream.ToArray();
|
|
}
|
|
DataObject clipboardData = new DataObject();
|
|
clipboardData.Set(DataFormats.Files, BitmapToBitmapImage(Image));
|
|
GlobalValues.Clipboard.SetDataObjectAsync(clipboardData);
|
|
}
|
|
else if (e.KeyModifiers == KeyModifiers.Control && e.Key == Key.S)
|
|
{
|
|
e.Handled = true;
|
|
btnSave_Click(sender, e);
|
|
}
|
|
}
|
|
|
|
|
|
} |