예제 #1
0
 public Page()
 {
   fImageHandler = new ImageHandler(this);
   fLayoutThumbnail = null;
   fImageBoundsInches = null;
   fSizeInch = null;
   fResolutionDpi = null;
 }
    public double Contrast;    // 0 to 1


    public DataSourceSettings()
    {
      ShowSettingsUI = false;
      ShowTransferUI = true;
      EnableFeeder = false;
      ColorMode = ColorModeEnum.BW;
      ScanArea = new BoundsInches(0, 0, SizeInches.Letter);
      Resolution = 200;
      Threshold = 0.5;
      Brightness = 0.5;
      Contrast = 0.5;
    }
    private static int GetTransformationIndex(TransformationMatrix transformationMatrix, BoundsInches imageBounds)
    {
      int[] vector = new int[4];

      vector[0] = GetSignOrZero(transformationMatrix.a);
      vector[1] = GetSignOrZero(transformationMatrix.b);
      vector[2] = GetSignOrZero(transformationMatrix.c);
      vector[3] = GetSignOrZero(transformationMatrix.d);

      int result = 0; // Default is to use the original picture

      for(int i = 0; i < 8; i++)
      {
        bool match = true;

        for(int j = 0; j < 4; j++)
        {
          if(fTransformationArray[i, j] != vector[j])
          {
            match = false;
          }
        }

        if(match)
        {
          result = i;
        }
      }

      // TODO: Calculate imageBounds
      imageBounds = null;

      return result;
    }
    private static TransformationMatrix GetTransformationMatrix(BoundsInches imageBounds, int transformationIndex)
    {
      TransformationMatrix result = new TransformationMatrix();

      result.a = fTransformationArray[transformationIndex, 0];
      result.b = fTransformationArray[transformationIndex, 1];
      result.c = fTransformationArray[transformationIndex, 2];
      result.d = fTransformationArray[transformationIndex, 3];
      result.e = fTransformationArray[transformationIndex, 4];
      result.f = fTransformationArray[transformationIndex, 5];

      result.a *= imageBounds.Width;
      result.b *= imageBounds.Height;
      result.c *= imageBounds.Width;
      result.d *= imageBounds.Height;
      result.e *= imageBounds.Width;
      result.e += imageBounds.X;
      result.f *= imageBounds.Height;
      result.f += imageBounds.Y;

      result.a *= 72;
      result.b *= 72;
      result.c *= 72;
      result.d *= 72;
      result.e *= 72;
      result.f *= 72;

      return result;
    }
    public static void AddJpegToPage(IntPtr document, IntPtr page, Image image, BoundsInches imageBounds, int transformationIndex)
    {
      byte[] buffer = Utils.Imaging.ByteArrayFromImage(image);

      // imageBounds are dimensions of transformed image (as displayed)
      TransformationMatrix matrix = GetTransformationMatrix(imageBounds, transformationIndex);

      unsafe
      {
        fixed(byte* p = buffer)
        {
          JpegInfo info = new JpegInfo();
          info.Height = image.Height;
          info.Width = image.Width;
          info.Data = (IntPtr)p;
          info.Size = buffer.Length;

          Pdfium.AddJpegImageToPage(document, page, &info, &matrix);
        }
      }
    }
    public static void AddBitmapToPage(IntPtr document, IntPtr page, Image image, BoundsInches imageBounds, int transformationIndex)
    {
      Bitmap bitmap = image as Bitmap;

      BitmapData bi = bitmap.LockBits(
          new Rectangle(0, 0, bitmap.Width, bitmap.Height),
          ImageLockMode.ReadOnly,
          bitmap.PixelFormat);

      PixelFormatEnum pf = ConvertPixelFormat_SystemToLibPdfium(bi.PixelFormat);

      TransformationMatrix matrix = GetTransformationMatrix(imageBounds, transformationIndex);

      try
      {
        unsafe
        {
          BitmapInfo info = new BitmapInfo();
          info.Height = bitmap.Height;
          info.Width = bitmap.Width;
          info.PixelFormat = pf;
          info.Data = bi.Scan0;
          info.Stride = bi.Stride;

          Pdfium.AddBitmapToPage(document, page, &info, &matrix);
        }
      }
      finally
      {
        bitmap.UnlockBits(bi);
      }
    }
예제 #7
0
    private void CalculateBounds()
    {
      SizePixels imageSizePixels = fImageHandler.SizePixels;
      SizeInches pageSizeInches = fSizeInch;

      fResolutionDpi = fImageHandler.ResolutionDpi;

      if(fResolutionDpi.IsDefined == false)
      {
        double image_aspect_ratio = imageSizePixels.Width / (double)imageSizePixels.Height;
        double page_aspect_ratio = pageSizeInches.Width / pageSizeInches.Height;

        double res;

        if(image_aspect_ratio > page_aspect_ratio)
        {
          // means our image has the width as the maximum dimension
          res = imageSizePixels.Width / pageSizeInches.Width;
        }
        else
        {
          // means our image has the height as the maximum dimension
          res = imageSizePixels.Height / pageSizeInches.Height;
        }

        fResolutionDpi = new ResolutionDpi(res);
      }

      // Calculate Image Layout
      double imageWidthInch = imageSizePixels.Width / fResolutionDpi.Horizontal;
      double imageHeightInch = imageSizePixels.Height / fResolutionDpi.Vertical;

      double x = (pageSizeInches.Width - imageWidthInch) / 2;
      double y = (pageSizeInches.Height - imageHeightInch) / 2;

      fImageBoundsInches = new BoundsInches(x, y, imageWidthInch, imageHeightInch);
    }
      private bool AdjustScannerSettings(
        int resolutionDpi,
        BoundsInches scanAreaInches,
        int brightnessPercents,
        int contrastPercents,
        int colorMode)
      {
        bool result;

        try
        {
          WiaUtils.SetProperty(fItem.Properties, WiaProperty.HorizontalResolution, resolutionDpi);
          WiaUtils.SetProperty(fItem.Properties, WiaProperty.VerticalResolution, resolutionDpi);
          WiaUtils.SetProperty(fItem.Properties, WiaProperty.HorizontalStartPosition, (int)(scanAreaInches.X * resolutionDpi));
          WiaUtils.SetProperty(fItem.Properties, WiaProperty.VerticalStartPosition, (int)(scanAreaInches.Y * resolutionDpi));
          WiaUtils.SetProperty(fItem.Properties, WiaProperty.HorizontalExtent, (int)(scanAreaInches.Width * resolutionDpi));
          WiaUtils.SetProperty(fItem.Properties, WiaProperty.VerticalExtent, (int)(scanAreaInches.Height * resolutionDpi));
          WiaUtils.SetProperty(fItem.Properties, WiaProperty.Brightness, brightnessPercents);
          WiaUtils.SetProperty(fItem.Properties, WiaProperty.Contrast, contrastPercents);
          WiaUtils.SetProperty(fItem.Properties, WiaProperty.CurrentIntent, colorMode);

          result = true;
        }
        catch
        {
          result = false;
        }

        return result;
      }