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; } /// /// Remove HTML from string with Regex. /// public static string StripTagsRegex(string source) { string test = source.Replace(" ", " "); return Regex.Replace(test, "<.*?>", string.Empty); } /// /// Compiled regular expression for performance. /// readonly Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled); /// /// Remove HTML from string with compiled Regex. /// public string StripTagsRegexCompiled(string source) { return _htmlRegex.Replace(source, string.Empty); } /// /// Remove HTML tags from string using char array. /// 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); } } }