forked from wyday/wyupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadWriteFiles.Client.cs
107 lines (91 loc) · 4.65 KB
/
ReadWriteFiles.Client.cs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Drawing;
using System.IO;
namespace wyUpdate.Common
{
public static partial class ReadFiles
{
//Image (for pre- 1.0 RC2 wyUpdate Client files)
public static Image ReadImage(Stream fs)
{
byte[] tempLength = new byte[4];
Image tempImg = null;
//skip the "length of data" int value
fs.Position += 4;
fs.Read(tempLength, 0, 4);
byte[] tempBytes = new byte[BitConverter.ToInt32(tempLength, 0)];
ReadWholeArray(fs, tempBytes);
try
{
using (MemoryStream ms = new MemoryStream(tempBytes, 0, tempBytes.Length))
{
tempImg = Image.FromStream(ms, true);
}
}
catch (Exception) { return null; /* blank image */ }
//copy the bitmap to be fully "in memory", so it's not referenced to the filestream
//(see http://support.microsoft.com/?id=814675 )
Bitmap tempBitmap = new Bitmap(tempImg, tempImg.Size);
Bitmap newBitmap = new Bitmap(tempBitmap.Width, tempBitmap.Height, tempBitmap.PixelFormat);
Rectangle rect = new Rectangle(new Point(0, 0), tempBitmap.Size);
//lock the two bitmaps
System.Drawing.Imaging.BitmapData origData =
tempBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
tempBitmap.PixelFormat);
System.Drawing.Imaging.BitmapData newData =
newBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
newBitmap.PixelFormat);
//calculate the size of the bitmap in memory
int imgSize = BitmapArraySize(tempBitmap.Width, tempBitmap.Height, tempBitmap.PixelFormat);
tempBytes = new byte[imgSize];
//copy the old "file referenced" bitmap to the new non-ref bitmap
System.Runtime.InteropServices.Marshal.Copy(origData.Scan0, tempBytes, 0, imgSize);
System.Runtime.InteropServices.Marshal.Copy(tempBytes, 0, newData.Scan0, imgSize);
//unlock the bits
tempBitmap.UnlockBits(origData);
newBitmap.UnlockBits(origData);
//destroy reference to the old bitmap
tempBitmap.Dispose();
return newBitmap;
}
// Finds the size of a Bitmap in memory given its pixel format
static int BitmapArraySize(int width, int height, System.Drawing.Imaging.PixelFormat pixFormat)
{
switch (pixFormat)
{
case System.Drawing.Imaging.PixelFormat.Extended:
case System.Drawing.Imaging.PixelFormat.Max:
case System.Drawing.Imaging.PixelFormat.Format64bppArgb:
case System.Drawing.Imaging.PixelFormat.Format64bppPArgb:
return width * height * 8;
case System.Drawing.Imaging.PixelFormat.Format48bppRgb:
return width * height * 6;
case System.Drawing.Imaging.PixelFormat.Canonical:
case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
case System.Drawing.Imaging.PixelFormat.Alpha:
case System.Drawing.Imaging.PixelFormat.PAlpha:
case System.Drawing.Imaging.PixelFormat.Indexed:
return width * height * 4;
case System.Drawing.Imaging.PixelFormat.Gdi:
case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
return width * height * 3;
case System.Drawing.Imaging.PixelFormat.Format16bppArgb1555:
case System.Drawing.Imaging.PixelFormat.Format16bppGrayScale:
case System.Drawing.Imaging.PixelFormat.Format16bppRgb555:
case System.Drawing.Imaging.PixelFormat.Format16bppRgb565:
return width * height * 2;
case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
return width * height;
case System.Drawing.Imaging.PixelFormat.Format4bppIndexed:
return (width * height) / 2;
case System.Drawing.Imaging.PixelFormat.Format1bppIndexed:
return (width * height) / 8;
case System.Drawing.Imaging.PixelFormat.Undefined:
default:
return -1;
}
}
}
}