Exemplo n.º 1
0
        private void addPageImage(PageObject page, string src, ushort width, ushort height)
        {
            // Load image and then save it out as a Jpeg (just so we know what
            // format the image is - we could of course just check the extension
            // but the LRF format only supports a few image types
            // Also JPeg seems to be better supported
            byte[] data = null;
            string file = src;

            if (file.StartsWith("http"))
            {
                // Grab image
                file = "c:\\image.jpg";
                System.Net.WebClient webClient = new System.Net.WebClient();
                webClient.DownloadFile(src, file);
            }

            file = file.Replace("file:///", "").Replace("%20", " ");
            if (!File.Exists(file))
            {
                return;
            }
            System.Drawing.Bitmap img = new System.Drawing.Bitmap(file);
            MemoryStream          mem = new MemoryStream();

            img.Save(mem, System.Drawing.Imaging.ImageFormat.Jpeg);
            data = mem.GetBuffer();
            mem.Close();
            img.Dispose();

            // Create a block Object
            BlockObject block = new BlockObject(GetNextObjId());

            block.BlockLink   = m_BlockAttr.ID;
            block.BlockHeight = height;
            block.BlockWidth  = width;
            block.BlockRule   = 34;
            m_Book.Objects.Add(block);

            // Create our ImageObject
            ImageObject io = new ImageObject(GetNextObjId());

            io.setRect(0, 0, width, height);
            io.setSize(width, height);
            m_Book.Objects.Add(io);

            // Center the image block on the page
            int left = (BBeB.ReaderPageWidth - width) / 2;

            if (left < 0)
            {
                left = 0;
            }
            page.StreamTags.Add(new UInt16Tag(TagId.LocationX, (ushort)left));
            page.StreamTags.Add(new UInt32Tag(TagId.Link, block.ID));

            // Set the image id in the block
            block.ObjectLink = io.ID;

            ushort streamId = GetNextObjId();

            io.setImageStreamId(streamId);

            // Create our ImageStream
            ImageStreamObject istream = new ImageStreamObject(streamId);

            istream.Data     = data;
            istream.Contents = StreamContents.JpegImage;
            m_Book.Objects.Add(istream);

            ObjectInfoObject objInfo = new ObjectInfoObject(GetNextObjId());

            objInfo.addLayoutTags(block.ID);
            m_Book.Objects.Add(objInfo);
            page.InfoObj = objInfo;

            // Now add out objects to the page
            page.Children.Add(block);
            page.Children.Add(io);
            page.Children.Add(m_BlockAttr);
            page.Children.Add(istream);
        }