Files
CustomerPanel-Test/libs/ConvertHelper.cs
2026-03-05 09:56:57 +01:00

103 lines
3.2 KiB
C#

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using C4IT_CustomerPanel.Properties;
namespace C4IT_CustomerPanel.libs
{
public static class ConverterHelper
{
public static System.Windows.Controls.Image ConvertImageToWpfImage(System.Drawing.Image image)
{
if (image == null)
throw new ArgumentNullException(nameof(image), Resources.Converter_ConvertImageToWpfImage_Image_darf_nicht_null_sein_);
using (System.Drawing.Bitmap dImg = new System.Drawing.Bitmap(image))
{
using (MemoryStream ms = new MemoryStream())
{
dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
bImg.BeginInit();
bImg.StreamSource = new MemoryStream(ms.ToArray());
bImg.EndInit();
System.Windows.Controls.Image img = new System.Windows.Controls.Image { Source = bImg };
return img;
}
}
}
public static System.Drawing.Image ConvertWpfImageToImage(System.Windows.Controls.Image image)
{
if (image == null)
throw new ArgumentNullException(nameof(image), Resources.Converter_ConvertImageToWpfImage_Image_darf_nicht_null_sein_);
System.Windows.Media.Imaging.BmpBitmapEncoder encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
MemoryStream ms = new MemoryStream();
encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create((System.Windows.Media.Imaging.BitmapSource)image.Source));
encoder.Save(ms);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
return img;
}
public static bool ObjectToBoolConverter(object source)
{
try
{
if (source == null)
return false;
if (source.Equals("1"))
{
source = 1;
}
return Convert.ToBoolean(source);
}
catch (Exception)
{
return false;
}
}
public static int ObjectToIntConverter(object source)
{
try
{
return Convert.ToInt32(source);
}
catch (Exception)
{
return 0;
}
}
public static string ObjectToStringConverter(this object value)
{
return (value ?? string.Empty).ToString();
}
public static Color ColorConvertFromString(string color)
{
var convertFromString = ColorConverter.ConvertFromString(color);
if (convertFromString != null)
{
return (Color)convertFromString;
}
return Colors.White;
}
}
}