1
#region PDFsharp - A .NET library for processing PDF
4
// Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
6
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
8
// http://www.pdfsharp.com
9
// http://sourceforge.net/projects/pdfsharp
11
// Permission is hereby granted, free of charge, to any person obtaining a
12
// copy of this software and associated documentation files (the "Software"),
13
// to deal in the Software without restriction, including without limitation
14
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
15
// and/or sell copies of the Software, and to permit persons to whom the
16
// Software is furnished to do so, subject to the following conditions:
18
// The above copyright notice and this permission notice shall be included
19
// in all copies or substantial portions of the Software.
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
// DEALINGS IN THE SOFTWARE.
31
using System.Diagnostics;
32
using System.Runtime.InteropServices;
34
using System.ComponentModel;
37
using System.Drawing.Drawing2D;
38
using System.Drawing.Imaging;
42
using System.Windows.Media;
43
using System.Windows.Media.Imaging;
45
using PdfSharp.Internal;
46
using PdfSharp.Fonts.TrueType;
48
using PdfSharp.Pdf.IO;
49
using PdfSharp.Pdf.Advanced;
52
#pragma warning disable 162
54
namespace PdfSharp.Drawing
57
/// Defines an object used to draw image files (bmp, png, jpeg, gif) and PDF forms.
58
/// An abstract base class that provides functionality for the Bitmap and Metafile descended classes.
60
public class XImage : IDisposable
63
/// Initializes a new instance of the <see cref="XImage"/> class.
71
/// Initializes a new instance of the <see cref="XImage"/> class from a GDI+ image.
75
this.gdiImage = image;
77
this.wpfImage = ImageHelper.CreateBitmapSource(image);
85
/// Initializes a new instance of the <see cref="XImage"/> class from a WPF image.
87
XImage(BitmapSource image)
89
this.wpfImage = image;
96
path = Path.GetFullPath(path);
97
if (!File.Exists(path))
98
throw new FileNotFoundException(PSSR.FileNotFound(path), path);
102
//FileStream file = new FileStream(filename, FileMode.Open);
103
//BitsLength = (int)file.Length;
104
//Bits = new byte[BitsLength];
105
//file.Read(Bits, 0, BitsLength);
108
this.gdiImage = Image.FromFile(path);
111
this.wpfImage = new BitmapImage(new Uri(path));
115
float vres = this.image.VerticalResolution;
116
float hres = this.image.HorizontalResolution;
117
SizeF size = this.image.PhysicalDimension;
118
int flags = this.image.Flags;
119
Size sz = this.image.Size;
120
GraphicsUnit units = GraphicsUnit.Millimeter;
121
RectangleF rect = this.image.GetBounds(ref units);
122
int width = this.image.Width;
127
XImage(Stream stream)
129
// Create a dummy unique path
130
this.path = "*" + Guid.NewGuid().ToString("B");
133
this.gdiImage = Image.FromStream(stream);
136
throw new NotImplementedException();
137
//this.wpfImage = new BitmapImage(new Uri(path));
141
float vres = this.image.VerticalResolution;
142
float hres = this.image.HorizontalResolution;
143
SizeF size = this.image.PhysicalDimension;
144
int flags = this.image.Flags;
145
Size sz = this.image.Size;
146
GraphicsUnit units = GraphicsUnit.Millimeter;
147
RectangleF rect = this.image.GetBounds(ref units);
148
int width = this.image.Width;
156
/// Implicit conversion from Image to XImage.
158
public static implicit operator XImage(Image image)
160
return new XImage(image);
165
/// Conversion from Image to XImage.
167
public static XImage FromGdiPlusImage(Image image)
169
return new XImage(image);
175
/// Conversion from BitmapSource to XImage.
177
public static XImage FromBitmapSource(BitmapSource image)
179
return new XImage(image);
184
/// Creates an image from the specified file.
186
/// <param name="path">The path to a BMP, PNG, GIF, JPEG, TIFF, or PDF file.</param>
187
public static XImage FromFile(string path)
189
if (PdfReader.TestPdfFile(path) > 0)
190
return new XPdfForm(path);
191
return new XImage(path);
195
/// Tests if a file exist. Supports PDF files with page number suffix.
197
/// <param name="path">The path to a BMP, PNG, GIF, JPEG, TIFF, or PDF file.</param>
198
public static bool ExistsFile(string path)
200
if (PdfReader.TestPdfFile(path) > 0)
202
return File.Exists(path);
208
if (this.gdiImage != null)
210
// ImageFormat has no overridden Equals...
211
string guid = this.gdiImage.RawFormat.Guid.ToString("B").ToUpper();
214
case "{B96B3CAA-0728-11D3-9D7B-0000F81EF32E}": // memoryBMP
215
case "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}": // bmp
216
case "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}": // png
217
this.format = XImageFormat.Png;
220
case "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}": // jpeg
221
this.format = XImageFormat.Jpeg;
224
case "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}": // gif
225
this.format = XImageFormat.Gif;
228
case "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}": // tiff
229
this.format = XImageFormat.Tiff;
232
case "{B96B3CB5-0728-11D3-9D7B-0000F81EF32E}": // icon
233
this.format = XImageFormat.Icon;
236
case "{B96B3CAC-0728-11D3-9D7B-0000F81EF32E}": // emf
237
case "{B96B3CAD-0728-11D3-9D7B-0000F81EF32E}": // wmf
238
case "{B96B3CB2-0728-11D3-9D7B-0000F81EF32E}": // exif
239
case "{B96B3CB3-0728-11D3-9D7B-0000F81EF32E}": // photoCD
240
case "{B96B3CB4-0728-11D3-9D7B-0000F81EF32E}": // flashPIX
243
throw new InvalidOperationException("Unsupported image format.");
249
if (this.wpfImage != null)
251
string pixelFormat = this.wpfImage.Format.ToString();
257
case "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}": // bmp
258
case "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}": // png
259
this.format = XImageFormat.Png;
262
case "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}": // jpeg
263
this.format = XImageFormat.Jpeg;
266
case "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}": // gif
271
this.format = XImageFormat.Gif;
274
case "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}": // tiff
275
this.format = XImageFormat.Tiff;
278
case "{B96B3CB5-0728-11D3-9D7B-0000F81EF32E}": // icon
279
this.format = XImageFormat.Icon;
282
case "{B96B3CAC-0728-11D3-9D7B-0000F81EF32E}": // emf
283
case "{B96B3CAD-0728-11D3-9D7B-0000F81EF32E}": // wmf
284
case "{B96B3CB2-0728-11D3-9D7B-0000F81EF32E}": // exif
285
case "{B96B3CB3-0728-11D3-9D7B-0000F81EF32E}": // photoCD
286
case "{B96B3CB4-0728-11D3-9D7B-0000F81EF32E}": // flashPIX
289
Debug.Assert(false, "Unknown pixel format: " + pixelFormat);
290
this.format = XImageFormat.Gif;
291
break;// throw new InvalidOperationException("Unsupported image format.");
298
/// under construction
300
public void Dispose()
303
//GC.SuppressFinalize(this);
307
/// Disposes underlying GDI+ object.
309
protected virtual void Dispose(bool disposing)
312
this.disposed = true;
315
if (this.gdiImage != null)
317
this.gdiImage.Dispose();
318
this.gdiImage = null;
328
/// Gets the width of the image.
330
[Obsolete("Use either PixelWidth or PointWidth. Temporarily obsolete because of rearrangements for WPF. Currently same as PixelWidth, but will become PointWidth in future releases of PDFsharp.")]
331
public virtual double Width
336
double gdiWidth = this.gdiImage.Width;
337
double wpfWidth = this.wpfImage.PixelWidth;
338
Debug.Assert(gdiWidth == wpfWidth);
342
return this.gdiImage.Width;
345
return this.wpfImage.PixelWidth;
351
/// Gets the height of the image.
353
[Obsolete("Use either PixelHeight or PointHeight. Temporarily obsolete because of rearrangements for WPF. Currently same as PixelHeight, but will become PointHeight in future releases of PDFsharp.")]
354
public virtual double Height
359
double gdiHeight = this.gdiImage.Height;
360
double wpfHeight = this.wpfImage.PixelHeight;
361
Debug.Assert(gdiHeight == wpfHeight);
365
return this.gdiImage.Height;
368
return this.wpfImage.PixelHeight;
374
/// Gets the width of the image in point.
376
public virtual double PointWidth
381
double gdiWidth = this.gdiImage.Width * 72 / this.gdiImage.HorizontalResolution;
382
double wpfWidth = this.wpfImage.Width * 72.0 / 96.0;
383
//Debug.Assert(gdiWidth == wpfWidth);
384
Debug.Assert(DoubleUtil.AreRoughlyEqual(gdiWidth, wpfWidth, 5));
388
return this.gdiImage.Width * 72 / this.gdiImage.HorizontalResolution;
391
Debug.Assert(Math.Abs(this.wpfImage.PixelWidth * 72 / this.wpfImage.DpiX - this.wpfImage.Width * 72.0 / 96.0) < 0.001);
392
return this.wpfImage.Width * 72.0 / 96.0;
398
/// Gets the height of the image in point.
400
public virtual double PointHeight
405
double gdiHeight = this.gdiImage.Height * 72 / this.gdiImage.HorizontalResolution;
406
double wpfHeight = this.wpfImage.Height * 72.0 / 96.0;
407
Debug.Assert(DoubleUtil.AreRoughlyEqual(gdiHeight, wpfHeight, 5));
411
return this.gdiImage.Height * 72 / this.gdiImage.HorizontalResolution;
414
Debug.Assert(Math.Abs(this.wpfImage.PixelHeight * 72 / this.wpfImage.DpiY - this.wpfImage.Height * 72.0 / 96.0) < 0.001);
415
return this.wpfImage.Height * 72.0 / 96.0;
421
/// Gets the width of the image in pixels.
423
public virtual int PixelWidth
428
int gdiWidth = this.gdiImage.Width;
429
int wpfWidth = this.wpfImage.PixelWidth;
430
Debug.Assert(gdiWidth == wpfWidth);
434
return this.gdiImage.Width;
437
return this.wpfImage.PixelWidth;
443
/// Gets the height of the image in pixels.
445
public virtual int PixelHeight
450
int gdiHeight = this.gdiImage.Height;
451
int wpfHeight = this.wpfImage.PixelHeight;
452
Debug.Assert(gdiHeight == wpfHeight);
456
return this.gdiImage.Height;
459
return this.wpfImage.PixelHeight;
465
/// Gets the size in point of the image.
467
public virtual XSize Size
469
get { return new XSize(PointWidth, PointHeight); }
473
/// Gets the horizontal resolution of the image.
475
public virtual double HorizontalResolution
480
double gdiResolution = this.gdiImage.HorizontalResolution;
481
double wpfResolution = this.wpfImage.PixelWidth * 96.0 / this.wpfImage.Width;
482
Debug.Assert(gdiResolution == wpfResolution);
483
return wpfResolution;
486
return this.gdiImage.HorizontalResolution;
489
return this.wpfImage.DpiX; //.PixelWidth * 96.0 / this.wpfImage.Width;
495
/// Gets the vertical resolution of the image.
497
public virtual double VerticalResolution
502
double gdiResolution = this.gdiImage.VerticalResolution;
503
double wpfResolution = this.wpfImage.PixelHeight * 96.0 / this.wpfImage.Height;
504
Debug.Assert(gdiResolution == wpfResolution);
505
return wpfResolution;
508
return this.gdiImage.VerticalResolution;
511
return this.wpfImage.DpiY; //.PixelHeight * 96.0 / this.wpfImage.Height;
517
/// Gets or sets a flag indicating whether image interpolation is to be performed.
519
public virtual bool Interpolate
521
get { return this.interpolate; }
522
set { this.interpolate = value; }
524
bool interpolate = true;
527
/// Gets the format of the image.
529
public XImageFormat Format
531
get { return this.format; }
537
internal void CreateAllImages(string name)
539
if (this.image != null)
541
this.image.Save(name + ".bmp", ImageFormat.Bmp);
542
this.image.Save(name + ".emf", ImageFormat.Emf);
543
this.image.Save(name + ".exif", ImageFormat.Exif);
544
this.image.Save(name + ".gif", ImageFormat.Gif);
545
this.image.Save(name + ".ico", ImageFormat.Icon);
546
this.image.Save(name + ".jpg", ImageFormat.Jpeg);
547
this.image.Save(name + ".png", ImageFormat.Png);
548
this.image.Save(name + ".tif", ImageFormat.Tiff);
549
this.image.Save(name + ".wmf", ImageFormat.Wmf);
550
this.image.Save(name + "2.bmp", ImageFormat.MemoryBmp);
555
internal Image gdiImage;
558
internal BitmapSource wpfImage;
562
/// If path starts with '*' the image is created from a stream and the path is a GUID.
564
internal string path;
567
/// Cache PdfImageTable.ImageSelector to speed up finding the right PdfImage
568
/// if this image is used more than once.
570
internal PdfImageTable.ImageSelector selector;
b'\\ No newline at end of file'