blob: 88ff54e5c6c8c708f45081295a1e11b2be550942 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BitmapUtilities.cs" company="HandBrake Project (http://handbrake.fr)">
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
// Defines the BitmapUtilities type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.Utilities
{
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Media.Imaging;
using HandBrake.Interop.Interop.Model.Preview;
/// <summary>
/// The bitmap utilities.
/// </summary>
public class BitmapUtilities
{
/// <summary>
/// Convert a Bitmap to a BitmapImagetype.
/// </summary>
/// <param name="bitmap">
/// The bitmap.
/// </param>
/// <returns>
/// The <see cref="BitmapImage"/>.
/// </returns>
public static BitmapImage ConvertToBitmapImage(Bitmap bitmap)
{
// Create a Bitmap Image for display.
using (var memoryStream = new MemoryStream())
{
try
{
bitmap.Save(memoryStream, ImageFormat.Bmp);
}
finally
{
bitmap.Dispose();
}
var wpfBitmap = new BitmapImage();
wpfBitmap.BeginInit();
wpfBitmap.CacheOption = BitmapCacheOption.OnLoad;
wpfBitmap.StreamSource = memoryStream;
wpfBitmap.EndInit();
wpfBitmap.Freeze();
return wpfBitmap;
}
}
public static Bitmap ConvertByteArrayToBitmap(RawPreviewData previewData)
{
var bitmap = new Bitmap(previewData.Width, previewData.Height);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, previewData.Width, previewData.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
IntPtr ptr = bitmapData.Scan0; // Pointer to the first pixel.
for (int i = 0; i < previewData.Height; i++)
{
try
{
Marshal.Copy(previewData.RawBitmapData, i * previewData.StrideWidth, ptr, previewData.StrideWidth);
ptr = IntPtr.Add(ptr, previewData.Width * 4);
}
catch (Exception exc)
{
Debug.WriteLine(exc); // In theory, this will allow a partial image display if this happens. TODO add better logging of this.
}
}
bitmap.UnlockBits(bitmapData);
return bitmap;
}
}
}
|