zhao
2021-06-04 c7ec496f9e41c2227103b3ef776e4a3f91bce6b2
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
//============================================================================
//Gios Pdf.NET - A library for exporting Pdf Documents in C#
//Copyright (C) 2005  Paolo Gios - www.paologios.com
//
//This 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 2.1 of the License, or (at your option) any later version.
//
//This 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 library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//=============================================================================
using System;
using System.Drawing;
using System.IO;
using System.Text;
 
namespace HH.WMS.Utils.Gios.Pdf
{
    /// <summary>
    /// a 72dpi jpeg based Image for a PdfPage
    /// </summary>
    public class PdfImage : PdfObject
    {
        internal MemoryStream ImageStream;
        string file;
        internal Bitmap bmp;
        /// <summary>
        /// gets the height of the loaded picture
        /// </summary>
        public int Height
        {
            get
            {
                return bmp.Height;
            }
        }
        /// <summary>
        /// gets the width of the loaded picture
        /// </summary>
        public int Width
        {
            get
            {
                return bmp.Width;
            }
        }
        
        internal PdfImage(int id,string file)
        {
            this.id=id;
            this.file=file;
            ImageStream=new MemoryStream();
            try
            {
                this.bmp=new Bitmap(file);
            }
            catch
            {
                throw new Exception("Error Loading Image File");
            }
        }
        
        internal int StreamGifWrite(System.IO.Stream stream)
        {
            FileStream fs;
            try
            {
                fs = File.OpenRead(this.file);
            }
            catch {throw new Exception("Can't open image file");}
            byte[] data = new byte[fs.Length];
                
            System.Drawing.Image i=Image.FromFile(file);
                
            string text1="";
            text1+=this.HeadObj;
            text1+="<</Type/XObject\n/Subtype/Image\n";
            text1+="/Width "+bmp.Width.ToString()+"\n/Height "+bmp.Height.ToString()+"\n";
            text1+="/BitsPerComponent 1\n";
            text1+="/Name/I"+this.ID+"\n";
            text1+="/ColorSpace/Indexed\n";
            //text1+="/Filter[/LZWDecode]\n";
            text1+="/Length "+data.Length.ToString()+"\n";
                
            text1+=">>\nstream\n";
            string text3="";
            text3+="\nendstream\n";
            text3+="endobj\n";
 
            Byte[] part1=ASCIIEncoding.ASCII.GetBytes(text1);
            fs.Read (data, 0, data.Length);
 
            Byte[] part3=ASCIIEncoding.ASCII.GetBytes(text3);
                
            stream.Write(part1,0,part1.Length);
            stream.Write(data,0,data.Length);
            stream.Write(part3,0,part3.Length);
            fs.Close();
            return part1.Length+data.Length+part3.Length;
        }
        internal override int StreamWrite(System.IO.Stream stream)
        {
            if (this.file.ToLower().EndsWith(".gif"))
            {
                return this.StreamGifWrite(stream);
            }
            
            FileStream fs;
            try
            {
                fs = File.OpenRead(this.file);
            }
            catch {throw new Exception("Can't open image file");}
            byte[] data = new byte[fs.Length];
                
            System.Drawing.Image i=Image.FromFile(file);
                
            //i.Save(this.ImageStream,System.Drawing.Imaging.ImageFormat.Jpeg);
            //((data=ImageStream.ToArray();
            string text1="";
            text1+=this.HeadObj;
            //text1+="<< /Length "+ImageStream.Length.ToString()+" /Filter /FlateDecode>>\n";
            text1+="<</Type/XObject\n/Subtype/Image\n";
            text1+="/Width "+bmp.Width.ToString()+"\n/Height "+bmp.Height.ToString()+"\n";
            text1+="/BitsPerComponent 8\n";
            text1+="/Name/I"+this.ID+"\n";
            text1+="/ColorSpace/DeviceRGB\n";
            //text1+="/Filter /FlateDecode\n";
            //text1+="/Resolution ["+bmp.HorizontalResolution.ToString("0.##").Replace(",",".");
            //text1+=" "+bmp.VerticalResolution.ToString("0.##").Replace(",",".")+"]\n";
            text1+="/Filter[/DCTDecode]\n";
            //text1+="/DecodeParms[<<>>]\n";
            text1+="/Length "+data.Length.ToString()+"\n";
                
            text1+=">>\nstream\n";
            string text3="";
            text3+="\nendstream\n";
            text3+="endobj\n";
 
            Byte[] part1=ASCIIEncoding.ASCII.GetBytes(text1);
            //Byte[] part2=sr.BaseStream.
            fs.Read (data, 0, data.Length);
 
            //Byte[] part2=this.ImageStream.ToArray();
            Byte[] part3=ASCIIEncoding.ASCII.GetBytes(text3);
                
            stream.Write(part1,0,part1.Length);
            stream.Write(data,0,data.Length);
            stream.Write(part3,0,part3.Length);
            fs.Close();
            return part1.Length+data.Length+part3.Length;
        }
 
    }
}