Example #1
0
        public IntPtr MarshalManagedToNative(object ManagedObj)
        {
            if (ManagedObj == null)
            {
                return(IntPtr.Zero);
            }

            String obj = ManagedObj as String;

            if (obj == null)
            {
                throw new MarshalDirectiveException(
                          "UTF8StringMarshaler must be used on a string.");
            }

            // not null terminated
            byte[] buffer        = Encoding.UTF8.GetBytes(obj);
            IntPtr nativeUTF8Str = DjvuMarshal.AllocHGlobal((uint)buffer.Length + 1);

            Marshal.Copy(buffer, 0, nativeUTF8Str, buffer.Length);

            // write the terminating null
            Marshal.WriteByte(nativeUTF8Str, buffer.Length, 0);
            return(nativeUTF8Str);
        }
Example #2
0
 public void CleanUpNativeData(IntPtr pNativeData)
 {
     try
     {
         DjvuMarshal.FreeHGlobal(pNativeData);
     }
     catch (AccessViolationException ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.ToString());
     }
 }
Example #3
0
        public IntPtr RenderPage(RenderMode mode, ref DjvuRectangle targetRect)
        {
            IntPtr format = NativeMethods.CreateDjvuFormat(FormatStyle.BGR24, 0, IntPtr.Zero);

            NativeMethods.SetDjvuFormatRowOrder(format, 1);
            NativeMethods.SetDjvuFormatYDirection(format, 1);
            NativeMethods.SetDjvuFormatDitherBits(format, 24);

            DjvuRectangle pageRect = new DjvuRectangle
            {
                X      = 0,
                Y      = 0,
                Height = (uint)Height,
                Width  = (uint)Width
            };

            IntPtr buffer = DjvuMarshal.AllocHGlobal((uint)(Width * 3 * Height));
            int    result = 0;

            try
            {
                result = NativeMethods.RenderDjvuPage(Page, mode, ref pageRect, ref targetRect,
                                                      format, (uint)Width * 3, buffer);

                if (result == 0)
                {
                    throw new DjvuLibreException(
                              $"Failed to render image at this time - result: {result}. Try later again.");
                }
            }
            catch (Exception ex)
            {
                DjvuMarshal.FreeHGlobal(buffer);
                buffer = IntPtr.Zero;
                NativeMethods.ReleaseDjvuFormat(format);
                format = IntPtr.Zero;

                string message = "Error while rendering page. Check InnerException message for details.";

                if (ex is DjvuLibreException)
                {
                    throw new DjvuLibreException(message, ex);
                }

                throw new AggregateException(message, ex);
            }

            return(buffer);
        }
Example #4
0
        public IntPtr RenderPage(RenderMode mode, ref DjvuRectangle pageRect, ref DjvuRectangle targetRect, IntPtr format)
        {
            if (pageRect.Width <= 0 || pageRect.Height <= 0)
            {
                pageRect = new DjvuRectangle
                {
                    X      = 0,
                    Y      = 0,
                    Height = (uint)Height,
                    Width  = (uint)Width
                };
            }

            IntPtr buffer = DjvuMarshal.AllocHGlobal((uint)(Width * 3 * Height));
            int    result = 0;

            try
            {
                result = NativeMethods.RenderDjvuPage(Page, mode, ref pageRect, ref targetRect,
                                                      format, (uint)Width * 3, buffer);

                if (result == 0)
                {
                    throw new DjvuLibreException(
                              $"Failed to render image this time - result: {result}. Try again later.");
                }
            }
            catch (Exception ex)
            {
                DjvuMarshal.FreeHGlobal(buffer);
                buffer = IntPtr.Zero;
                NativeMethods.ReleaseDjvuFormat(format);
                format = IntPtr.Zero;

                string message = "Error while rendering page. Check InnerException message for details.";

                if (ex is DjvuLibreException)
                {
                    throw new DjvuLibreException(message, ex);
                }

                throw new AggregateException(message, ex);
            }

            return(buffer);
        }