示例#1
0
 internal static MatrixMu Concat(MatrixMu one, MatrixMu two)
 {
     return(new MatrixMu(
                one.A * two.A + one.B * two.C,
                one.A * two.B + one.B * two.D,
                one.C * two.A + one.D * two.C,
                one.C * two.B + one.D * two.D,
                one.E * two.A + one.F * two.C + two.E,
                one.E * two.B + one.F * two.D + two.F));
 }
 public static extern void RunPage(IntPtr doc, IntPtr page, IntPtr dev, ref MatrixMu transform, IntPtr cookie);
        private static Bitmap RenderPage(IntPtr context, IntPtr document, IntPtr page, RectangleMu pageBound, float zoomX = 1.0f, float zoomY = 1.0f)
        {
            MatrixMu ctm = new MatrixMu();
            IntPtr   pix = IntPtr.Zero;
            IntPtr   dev = IntPtr.Zero;

            // gets the size of the scaled page
            int width  = GetBitmapWidth(pageBound, zoomX);  // (int)(zoomX * (pageBound.Right - pageBound.Left));
            int height = GetBitmapHeight(pageBound, zoomY); // (int)(zoomY * (pageBound.Bottom - pageBound.Top));

            ctm.A = zoomX;
            ctm.D = zoomY; // sets the matrix as (zoomX,0,0,zoomY,0,0)

            // creates a pixmap the same size as the width and height of the page
            pix = NativeMethods.NewPixmap(context, NativeMethods.LookupDeviceColorSpace(context, "DeviceRGB"), width, height);
            // sets white color as the background color of the pixmap
            NativeMethods.ClearPixmap(context, pix, 0xFF);

            // creates a drawing device
            dev = NativeMethods.NewDrawDevice(context, pix);
            // draws the page on the device created from the pixmap
            NativeMethods.RunPage(document, page, dev, ref ctm, IntPtr.Zero);

            NativeMethods.FreeDevice(dev); // frees the resources consumed by the device
            dev = IntPtr.Zero;

            // creates a colorful bitmap of the same size of the pixmap
            Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            //bmp.SetResolution(300, 300);
            var imageData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bmp.PixelFormat);

            unsafe
            { // converts the pixmap data to Bitmap data
              // gets the rendered data from the pixmap
                byte *ptrSrc  = (byte *)NativeMethods.GetSamples(context, pix);
                byte *ptrDest = (byte *)imageData.Scan0;
                for (int y = 0; y < height; y++)
                {
                    byte *pl = ptrDest;
                    byte *sl = ptrSrc;
                    for (int x = 0; x < width; x++)
                    {
                        //Swap these here instead of in MuPDF because most pdf images will be rgb or cmyk.
                        //Since we are going through the pixels one by one
                        //anyway swap here to save a conversion from rgb to bgr.
                        pl[2] = sl[0]; //b-r
                        pl[1] = sl[1]; //g-g
                        pl[0] = sl[2]; //r-b
                                       //sl[3] is the alpha channel, we will skip it here
                        pl += 3;
                        sl += 4;
                    }
                    ptrDest += imageData.Stride;
                    ptrSrc  += width * 4;
                }
            }
            // free bitmap in memory
            bmp.UnlockBits(imageData);
            NativeMethods.DropPixmap(context, pix);
            return(bmp);
        }