You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.8 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
namespace Txgy.EWS.Client.Common.Helpers
{
public static class BitmapHelper
{
public static BitmapImage ConvertToBitmap(byte[] bytes)
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(bytes);
bitmapImage.EndInit();
return bitmapImage;
}
public static byte[] ConvertToBytes(BitmapSource bitmapSource)
{
byte[] buffer = null;
PngBitmapEncoder encoder = new PngBitmapEncoder();
MemoryStream memoryStream = new MemoryStream();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
encoder.Save(memoryStream);
memoryStream.Position = 0;
if (memoryStream.Length > 0)
{
using (BinaryReader br = new BinaryReader(memoryStream))
{
buffer = br.ReadBytes((int)memoryStream.Length);
}
}
memoryStream.Close();
return buffer;
}
public static BitmapImage ConvertToBitmap(BitmapSource bitmapSource)
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
MemoryStream memoryStream = new MemoryStream();
BitmapImage bImg = new BitmapImage();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
encoder.Save(memoryStream);
memoryStream.Position = 0;
bImg.BeginInit();
bImg.StreamSource = memoryStream;
bImg.EndInit();
memoryStream.Close();
return bImg;
}
public static byte[] BitmapImageToBytes(BitmapImage bmp)
{
byte[] buffer = null;
try
{
Stream stream = bmp.StreamSource;
if (stream != null && stream.Length > 0)
{
//很重要因为Position经常位于Stream的末尾导致下面读取到的长度为0。
stream.Position = 0;
using (BinaryReader br = new BinaryReader(stream))
{
buffer = br.ReadBytes((int)stream.Length);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return buffer;
}
public static byte[] ElementToBytes(UIElement element)
{
var rect = new Rect(element.RenderSize);
var visual = new DrawingVisual();
using (var dc = visual.RenderOpen())
{
dc.DrawRectangle(new VisualBrush(element), null, rect);
}
var bitmap = new RenderTargetBitmap(
(int)rect.Width, (int)rect.Height, 96, 96, PixelFormats.Default);
bitmap.Render(visual);
return BitmapHelper.ConvertToBytes(bitmap);
}
public static void SaveImageToFile(UIElement element, string fileName)
{
var rect = new Rect(element.RenderSize);
System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
RenderTargetBitmap bmp = new RenderTargetBitmap((int)rect.X, (int)rect.Y, 96d, 96d, PixelFormats.Pbgra32);
bmp.Render(element);
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(fs);
fs.Close();
}
}
}