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

106 lines
3.5 KiB
C#

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Data;
using System.Windows.Markup;
using C4IT_CustomerPanel.Properties;
namespace C4IT_CustomerPanel.libs
{
public class Converter
{
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;
}
/// <summary>
/// Remove HTML from string with Regex.
/// </summary>
public static string StripTagsRegex(string source)
{
string test = source.Replace("&nbsp;", " ");
return Regex.Replace(test, "<.*?>", string.Empty);
}
/// <summary>
/// Compiled regular expression for performance.
/// </summary>
readonly Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
/// <summary>
/// Remove HTML from string with compiled Regex.
/// </summary>
public string StripTagsRegexCompiled(string source)
{
return _htmlRegex.Replace(source, string.Empty);
}
/// <summary>
/// Remove HTML tags from string using char array.
/// </summary>
public string StripTagsCharArray(string source)
{
char[] array = new char[source.Length];
int arrayIndex = 0;
bool inside = false;
foreach (char @let in source)
{
if (@let == '<')
{
inside = true;
continue;
}
if (@let == '>')
{
inside = false;
continue;
}
if (!inside)
{
array[arrayIndex] = @let;
arrayIndex++;
}
}
return new string(array, 0, arrayIndex);
}
}
}