Jypeli 10
The simple game programming library
Screencap.cs
Siirry tämän tiedoston dokumentaatioon.
1using System.Drawing;
2using System.Drawing.Imaging;
3using System.IO;
4using System.Linq;
5
6namespace Jypeli
7{
8 internal static class Screencap
9 {
10 public static void WriteBmp( Stream outStream, Jypeli.Image image )
11 {
12 // TODO: Kuva tulee ylösalaisin
13 byte[] buffer = image.GetByteArray();
14
15 BinaryWriter writer = new BinaryWriter(outStream);
16 {
17 // Magic
18 writer.Write('B');
19 writer.Write('M');
20
21 // Size of the BMP file
22 //writer.Write( (int)(54 + buffer.Length) );
23 writer.Write((int)(122 + buffer.Length));
24
25 // Unused
26 writer.Write( (int)0 );
27
28 // Offset of pixel array
29 //writer.Write( (int)0x36 );
30 writer.Write((int)0x7A);
31
32 // DIB header length
33 //writer.Write( (int)0x28 );
34 writer.Write( (int)108 );
35
36 // Image dimensions
37 writer.Write( image.Width );
38 writer.Write( image.Height );
39
40 // Number of color planes
41 writer.Write( (short)1 );
42
43 // Bits per pixel
44 writer.Write( (short)32 );
45
46 // No compression, specify bitfields
47 //writer.Write( (int)0 );
48 writer.Write( (int)3 );
49
50 // Size of raw bitmap data
51 writer.Write( (int)buffer.Length );
52
53 // Print resolution as pixels per meter (this is 72 dpi)
54 writer.Write( (int)2835 );
55 writer.Write( (int)2835 );
56
57 // Number of colors in the palette
58 writer.Write( (int)0 );
59
60 // Number of important colors
61 writer.Write( (int)0 );
62
63 // Bit masks: red, green, blue, alpha (big endian)
64 writer.Write( new byte[] { 0xFF, 0x00, 0x00, 0x00 } );
65 writer.Write( new byte[] { 0x00, 0xFF, 0x00, 0x00 } );
66 writer.Write( new byte[] { 0x00, 0x00, 0xFF, 0x00 } );
67 writer.Write( new byte[] { 0x00, 0x00, 0x00, 0xFF } );
68
69 // Color space: "Win"
70 writer.Write(' ');
71 writer.Write('n');
72 writer.Write('i');
73 writer.Write('W');
74
75 // Unused (color space endpoints & gamma)
76 writer.Write( new byte[36] );
77 writer.Write( (int)0 );
78 writer.Write( (int)0 );
79 writer.Write( (int)0 );
80
81 // Bitmap data
82 writer.Write(buffer);
83
84 }
85
86 // Do not call Close or Dispose! It closes the stream too.
87 writer.Flush();
88 }
89
90 public static void EncodePng(Stream outStream, Stream inStream)
91 {
92 var encoders = ImageCodecInfo.GetImageEncoders();
93
94 Bitmap bmp = new Bitmap(inStream);
95 bmp.Save(outStream, ImageFormat.Png);
96 }
97
98 public static void EncodeJpeg(Stream outStream, Stream inStream)
99 {
100 var bitmap = new Bitmap(inStream);
101
102 ImageCodecInfo jpgEncoder = GetCodecByDescription("JPEG");
103 Encoder encoder2 = System.Drawing.Imaging.Encoder.Quality;
104 EncoderParameters parameters = new System.Drawing.Imaging.EncoderParameters(1);
105 EncoderParameter parameter = new EncoderParameter(encoder2, 50L);
106 parameters.Param[0] = parameter;
107
108 //System.IO.Stream stream = new MemoryStream();
109 //bitmap.Save(stream, jpgEncoder, parameters);
110 bitmap.Save(outStream, jpgEncoder, parameters);
111
112 /*var bytes = ((MemoryStream)stream).ToArray();
113 System.IO.Stream inputStream = new MemoryStream(bytes);
114 Bitmap fromDisk = new Bitmap(@"C:\Temp\TestJPEG.jpg");
115 Bitmap fromStream = new Bitmap(inputStream);*/
116 }
117
123 public static void SaveJPG(string fname, Image img)
124 {
125 using (var image = System.Drawing.Image.FromStream(img.AsJpeg()))
126 {
127 image.Save(fname);
128 }
129 }
130
131 private static ImageCodecInfo GetCodecByDescription( string desc )
132 {
133 foreach ( var codec in ImageCodecInfo.GetImageEncoders().FindAll(x => x.FormatDescription == "JPEG") )
134 {
135 return codec;
136 }
137
138 throw new IOException(desc + " codec not found");
139 }
140 }
141}
Kuva.
Definition: Image.cs:30
Stream AsJpeg()
Palauttaa kuvan jpeg-muodossa, jossa se voidaan esimerkiksi tallentaa DataStorage....
Definition: Image.cs:1225
static void WriteBmp(Stream outStream, Jypeli.Image image)
Definition: Screencap.cs:10
static void EncodeJpeg(Stream outStream, Stream inStream)
Definition: Screencap.cs:98
static void EncodePng(Stream outStream, Stream inStream)
Definition: Screencap.cs:90
static ImageCodecInfo GetCodecByDescription(string desc)
Definition: Screencap.cs:131
static void SaveJPG(string fname, Image img)
Tallentaa kuvan jpg-muodossa.
Definition: Screencap.cs:123