/
ImageNode.cs
310 lines (266 loc) · 10.6 KB
/
ImageNode.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/********************************************************************************
* This file is part of NRtfTree Library.
*
* NRtfTree Library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* NRtfTree Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
********************************************************************************/
/********************************************************************************
* Library: NRtfTree
* Version: v0.3.0
* Date: 02/09/2007
* Copyright: 2007 Salvador Gomez
* E-mail: sgoliver.net@gmail.com
* Home Page: http://www.sgoliver.net
* SF Project: http://nrtftree.sourceforge.net
* http://sourceforge.net/projects/nrtftree
* Class: ImageNode
* Description: Nodo RTF especializado que contiene la información de una imagen.
* ******************************************************************************/
using System;
using System.Text;
using Net.Sgoliver.NRtfTree.Core;
using System.IO;
using System.Globalization;
using System.Drawing;
using System.Drawing.Imaging;
namespace Net.Sgoliver.NRtfTree
{
namespace Util
{
/// <summary>
/// Encapsula un nodo RTF de tipo Imagen (Palabra clave "\pict")
/// </summary>
public class ImageNode : Net.Sgoliver.NRtfTree.Core.RtfTreeNode
{
#region Atributos privados
/// <summary>
/// Array de bytes con la información de la imagen.
/// </summary>
private byte[] data;
#endregion
#region Constructores
/// <summary>
/// Constructor de la clase ImageNode.
/// </summary>
/// <param name="node">Nodo RTF del que se obtendrán los datos de la imagen.</param>
public ImageNode(RtfTreeNode node)
{
if(node != null)
{
//Asignamos todos los campos del nodo
this.NodeKey = node.NodeKey;
this.HasParameter = node.HasParameter;
this.Parameter = node.Parameter;
this.ParentNode = node.ParentNode;
this.RootNode = node.RootNode;
this.NodeType = node.NodeType;
this.ChildNodes.Clear();
this.ChildNodes.AddRange(node.ChildNodes);
//Obtenemos los datos de la imagen como un array de bytes
getImageData();
}
}
#endregion
#region Propiedades
/// <summary>
/// Devuelve una cadena de caracteres con el contenido de la imagen en formato hexadecimal.
/// </summary>
public string HexData
{
get
{
return this.SelectSingleChildNode(RtfNodeType.Text).NodeKey;
}
}
/// <summary>
/// Devuelve el formato original de la imagen.
/// </summary>
public System.Drawing.Imaging.ImageFormat ImageFormat
{
get
{
if (this.SelectSingleChildNode("jpegblip") != null)
return System.Drawing.Imaging.ImageFormat.Jpeg;
else if (this.SelectSingleChildNode("pngblip") != null)
return System.Drawing.Imaging.ImageFormat.Png;
else if (this.SelectSingleChildNode("emfblip") != null)
return System.Drawing.Imaging.ImageFormat.Emf;
else if (this.SelectSingleChildNode("wmetafile") != null)
return System.Drawing.Imaging.ImageFormat.Wmf;
else if (this.SelectSingleChildNode("dibitmap") != null || this.SelectSingleChildNode("wbitmap") != null)
return System.Drawing.Imaging.ImageFormat.Bmp;
else
return null;
}
}
/// <summary>
/// Devuelve el ancho de la imagen (en twips).
/// </summary>
public int Width
{
get
{
RtfTreeNode node = this.SelectSingleChildNode("picw");
if (node != null)
return node.Parameter;
else
return -1;
}
}
/// <summary>
/// Devuelve el alto de la imagen (en twips).
/// </summary>
public int Height
{
get
{
RtfTreeNode node = this.SelectSingleChildNode("pich");
if (node != null)
return node.Parameter;
else
return -1;
}
}
/// <summary>
/// Devuelve el ancho objetivo de la imagen (en twips).
/// </summary>
public int DesiredWidth
{
get
{
RtfTreeNode node = this.SelectSingleChildNode("picwgoal");
if (node != null)
return node.Parameter;
else
return -1;
}
}
/// <summary>
/// Devuelve el alto objetivo de la imagen (en twips).
/// </summary>
public int DesiredHeight
{
get
{
RtfTreeNode node = this.SelectSingleChildNode("pichgoal");
if (node != null)
return node.Parameter;
else
return -1;
}
}
/// <summary>
/// Devuelve la escala horizontal de la imagen, en porcentaje.
/// </summary>
public int ScaleX
{
get
{
RtfTreeNode node = this.SelectSingleChildNode("picescalex");
if (node != null)
return node.Parameter;
else
return -1;
}
}
/// <summary>
/// Devuelve la escala vertical de la imagen, en porcentaje.
/// </summary>
public int ScaleY
{
get
{
RtfTreeNode node = this.SelectSingleChildNode("picescaley");
if (node != null)
return node.Parameter;
else
return -1;
}
}
#endregion
#region Metodos Publicos
/// <summary>
/// Devuelve un array de bytes con el contenido de la imagen.
/// </summary>
/// <return>Array de bytes con el contenido de la imagen.</return>
public byte[] GetByteData()
{
return data;
}
/// <summary>
/// Guarda una imagen a fichero con el formato original.
/// </summary>
/// <param name="filePath">Ruta del fichero donde se guardará la imagen.</param>
public void SaveImage(string filePath)
{
if (data != null)
{
MemoryStream stream = new MemoryStream(data, 0, data.Length);
//Escribir a un fichero cualquier tipo de imagen
Bitmap bitmap = new Bitmap(stream);
bitmap.Save(filePath, this.ImageFormat);
}
}
/// <summary>
/// Guarda una imagen a fichero con un formato determinado indicado como parámetro.
/// </summary>
/// <param name="filePath">Ruta del fichero donde se guardará la imagen.</param>
/// <param name="format">Formato con el que se escribirá la imagen.</param>
public void SaveImage(string filePath, System.Drawing.Imaging.ImageFormat format)
{
if (data != null)
{
MemoryStream stream = new MemoryStream(data, 0, data.Length);
//System.Drawing.Imaging.Metafile metafile = new System.Drawing.Imaging.Metafile(stream);
//Escribir directamente el array de bytes a un fichero ".jpg"
//FileStream fs = new FileStream("c:\\prueba.jpg", FileMode.CreateNew);
//BinaryWriter w = new BinaryWriter(fs);
//w.Write(image,0,imageSize);
//w.Close();
//fs.Close();
//Escribir a un fichero cualquier tipo de imagen
Bitmap bitmap = new Bitmap(stream);
bitmap.Save(filePath, format);
}
}
#endregion
#region Metodos privados
/// <summary>
/// Obtiene los datos de la imagen a partir de la información contenida en el nodo RTF.
/// </summary>
private void getImageData()
{
//Formato 1 (Word 97-2000): {\*\shppict {\pict\jpegblip <datos>}}{\nonshppict {\pict\wmetafile8 <datos>}}
//Formato 2 (Wordpad) : {\pict\wmetafile8 <datos>}
string Text = "";
if (this.FirstChild.NodeKey == "pict")
{
Text = this.SelectSingleChildNode(RtfNodeType.Text).NodeKey;
int dataSize = Text.Length / 2;
data = new byte[dataSize];
StringBuilder sbaux = new StringBuilder(2);
for (int i = 0; i < Text.Length; i++)
{
sbaux.Append(Text[i]);
if (sbaux.Length == 2)
{
data[i / 2] = byte.Parse(sbaux.ToString(), NumberStyles.HexNumber);
sbaux.Remove(0, 2);
}
}
}
}
#endregion
}
}
}