Example #1
0
        private Size CalculatePageImageSize()
        {
            try
            {
                // check and convert page format units to selected units
                double selectedFormatWidth  = selectedFormat.Width();
                double selectedFormatHeight = selectedFormat.Height();

                if (selectedFormat.Units() != selectedUnit)
                {
                    if (selectedUnit == cm)
                    {
                        selectedFormatWidth  = UoM.ConvertInchToCM(selectedFormatWidth);
                        selectedFormatHeight = UoM.ConvertInchToCM(selectedFormatHeight);
                    }
                    else
                    {
                        selectedFormatWidth  = UoM.ConvertCMtoInch(selectedFormatWidth);
                        selectedFormatHeight = UoM.ConvertCMtoInch(selectedFormatHeight);
                    }
                }

                // account for margin
                double imageWidth  = selectedFormatWidth - marginSides * 2;
                double imageHeight = selectedFormatHeight - marginTop * 2;

                // calculate pixel dimensions
                int width  = selectedUnit.ToPixels(imageWidth);
                int height = selectedUnit.ToPixels(imageHeight);

                Size size = new Size(width, height);

                return(size);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

                return(new Size(0, 0));
            }
        }
Example #2
0
        private void GeneratePageImages(Size imageSize, double xScale, double yScale)
        {
            // clear old lists
            imageListPages.Images.Clear();
            pagesList.Clear();
            listViewPrintPreview.Clear();

            foreach (Image p in generatedPagesList)
            {
                p.Dispose();
            }

            generatedPagesList.Clear();

            // get size for cutting before resizing to page size
            Size cutImageSize = new Size((int)(imageSize.Width / xScale), (int)(imageSize.Height / xScale));

            Console.WriteLine("Calculated cut size from preview image: " + cutImageSize);

            // get overlap values in pixels to scale
            int pixelOverlapTop   = (int)(selectedUnit.ToPixels(overlapTop) / yScale);
            int pixelOverlapSides = (int)(selectedUnit.ToPixels(overlapSides) / xScale);

            // get margin values
            int pixelMarginTop   = selectedUnit.ToPixels(marginTop);
            int pixelMarginSides = selectedUnit.ToPixels(marginSides);

            // check and convert page format units to selected units
            double selectedFormatWidth  = selectedFormat.Width();
            double selectedFormatHeight = selectedFormat.Height();

            if (selectedFormat.Units() != selectedUnit)
            {
                if (selectedUnit == cm)
                {
                    selectedFormatWidth  = UoM.ConvertInchToCM(selectedFormatWidth);
                    selectedFormatHeight = UoM.ConvertInchToCM(selectedFormatHeight);
                }
                else
                {
                    selectedFormatWidth  = UoM.ConvertCMtoInch(selectedFormatWidth);
                    selectedFormatHeight = UoM.ConvertCMtoInch(selectedFormatHeight);
                }
            }

            // get graphics of edit image
            Graphics editGraphics = Graphics.FromImage(editImage);

            // create rectangle for cutting and drawing
            Rectangle cutRectangle = new Rectangle(0, 0, cutImageSize.Width, cutImageSize.Height);

            // create pen for drawing on preview
            Pen pen = new Pen(Color.Red, 2);

            // for entire height of image
            for (int y = 0; y < editImage.Height; y += (cutImageSize.Height - pixelOverlapTop))
            {
                // for each width of image
                for (int x = 0; x < editImage.Width; x += (cutImageSize.Width - pixelOverlapSides))
                {
                    cutRectangle.X = x;

                    cutRectangle.Y = y;

                    editGraphics.DrawRectangle(pen, cutRectangle);

                    // get cut out
                    Image cutout = new Bitmap(cutImageSize.Width, cutImageSize.Height);
                    using (Graphics g = Graphics.FromImage(cutout))
                    {
                        g.DrawImage(editImage, 0, 0, cutRectangle, GraphicsUnit.Pixel);

                        g.Dispose();
                    }

                    // resize cut out
                    Image newCutout = ResizeImage(cutout, imageSize.Width, imageSize.Height);

                    // create full page image
                    Image fullPageImage = new Bitmap(selectedUnit.ToPixels(selectedFormatWidth), selectedUnit.ToPixels(selectedFormatHeight));

                    using (Graphics g = Graphics.FromImage(fullPageImage))
                    {
                        // make page white
                        g.FillRectangle(Brushes.White, 0, 0, fullPageImage.Width, fullPageImage.Height);

                        // draw cutout onto page
                        g.DrawImage(newCutout, pixelMarginSides, pixelMarginTop);

                        g.Dispose();
                    }

                    generatedPagesList.Add(fullPageImage);


                    // create thumbnail of image
                    Bitmap thumb = GenerateThumbnail(fullPageImage);
                    this.imageListPages.Images.Add(thumb);

                    cutout.Dispose();
                    newCutout.Dispose();
                    // fullPageImage.Dispose();
                }
            }

            this.listViewPrintPreview.LargeImageList = this.imageListPages;

            for (int j = 0; j < this.imageListPages.Images.Count; j++)
            {
                ListViewItem item = new ListViewItem();
                item.ImageIndex = j;
                this.listViewPrintPreview.Items.Add(item);
            }

            editGraphics.Dispose();
        }