示例#1
0
		public override bool ProcessCapture(ISurface surface, ICaptureDetails captureDetails) {
			LOG.DebugFormat("Changing surface to grayscale!");
			using (BitmapBuffer bbb = new BitmapBuffer(surface.Image as Bitmap, false)) {
				bbb.Lock();
				for(int y=0;y<bbb.Height; y++) {
					for(int x=0;x<bbb.Width; x++) {
						Color color = bbb.GetColorAt(x, y);
						int luma  = (int)((0.3*color.R) + (0.59*color.G) + (0.11*color.B));
						color = Color.FromArgb(luma, luma, luma);
						bbb.SetColorAt(x, y, color);
					}
				}
			}

			return true;
		}
        public static void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, int pixelSize)
        {
            if(pixelSize <= 1 || rect.Width == 0 || rect.Height == 0) {
                // Nothing to do
                return;
            }
            if(rect.Width < pixelSize) pixelSize = rect.Width;
            if(rect.Height < pixelSize) pixelSize = rect.Height;

            using (BitmapBuffer bbbDest = new BitmapBuffer(applyBitmap, rect)) {
                bbbDest.Lock();
                using(BitmapBuffer bbbSrc = new BitmapBuffer(applyBitmap, rect)) {
                    bbbSrc.Lock();
                    List<Color> colors = new List<Color>();
                    int halbPixelSize = pixelSize/2;
                    for(int y=-halbPixelSize;y<bbbSrc.Height+halbPixelSize; y=y+pixelSize) {
                        for(int x=-halbPixelSize;x<=bbbSrc.Width+halbPixelSize; x=x+pixelSize) {
                            colors.Clear();
                            for(int yy=y;yy<y+pixelSize;yy++) {
                                if (yy >=0 && yy < bbbSrc.Height) {
                                    for(int xx=x;xx<x+pixelSize;xx++) {
                                        colors.Add(bbbSrc.GetColorAt(xx,yy));
                                    }
                                }
                            }
                            Color currentAvgColor = Colors.Mix(colors);
                            for(int yy=y;yy<=y+pixelSize;yy++) {
                                if (yy >=0 && yy < bbbSrc.Height) {
                                    for(int xx=x;xx<=x+pixelSize;xx++) {
                                        bbbDest.SetColorAt(xx, yy, currentAvgColor);
                                    }
                                }
                            }
                        }
                    }
                }
                bbbDest.DrawTo(graphics, rect.Location);
            }
        }
 /// <summary>
 /// Return negative of Bitmap
 /// </summary>
 /// <param name="sourceBitmap">Bitmap to create a negative off</param>
 /// <returns>Negative bitmap</returns>
 public static Bitmap CreateNegative(Bitmap sourceBitmap)
 {
     using (BitmapBuffer bb = new BitmapBuffer(sourceBitmap, true)) {
         bb.Lock();
         for (int y = 0; y < bb.Height; y++) {
             for (int x = 0; x < bb.Width; x++) {
                 Color color = bb.GetColorAt(x, y);
                 Color invertedColor = Color.FromArgb(color.A, color.R ^ 255, color.G ^ 255, color.B ^ 255);
                 bb.SetColorAt(x, y, invertedColor);
             }
         }
         bb.Unlock();
         return bb.Bitmap;
     }
 }
示例#4
0
        void DrawImageForPrint(object sender, PrintPageEventArgs e)
        {
            PrintOptionsDialog pod = printOptionsDialog;

            ContentAlignment alignment = pod.AllowPrintCenter ? ContentAlignment.MiddleCenter : ContentAlignment.TopLeft;

            if (conf.OutputPrintInverted) {
                // Invert Bitmap
                BitmapBuffer bb = new BitmapBuffer((Bitmap)image, false);
                bb.Lock();
                for(int y=0;y<bb.Height; y++) {
                    for(int x=0;x<bb.Width; x++) {
                        Color color = bb.GetColorAt(x, y);
                        Color invertedColor = Color.FromArgb(color.A, color.R ^ 255, color.G ^ 255, color.B ^ 255);
                        bb.SetColorAt(x, y, invertedColor);
                    }
                }
                bb.Dispose();
            }

            RectangleF pageRect = e.PageSettings.PrintableArea;
            GraphicsUnit gu = GraphicsUnit.Pixel;
            RectangleF imageRect = image.GetBounds(ref gu);
            // rotate the image if it fits the page better
            if(pod.AllowPrintRotate) {
                if((pageRect.Width > pageRect.Height && imageRect.Width < imageRect.Height) ||
                   (pageRect.Width < pageRect.Height && imageRect.Width > imageRect.Height)) {
                    image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    imageRect = image.GetBounds(ref gu);
                    if(alignment.Equals(ContentAlignment.TopLeft)) alignment = ContentAlignment.TopRight;
                }
            }
            RectangleF printRect = new RectangleF(0,0,imageRect.Width, imageRect.Height);;
            // scale the image to fit the page better
            if(pod.AllowPrintEnlarge || pod.AllowPrintShrink) {
                SizeF resizedRect = ScaleHelper.GetScaledSize(imageRect.Size,pageRect.Size,false);
                if((pod.AllowPrintShrink && resizedRect.Width < printRect.Width) ||
                   pod.AllowPrintEnlarge && resizedRect.Width > printRect.Width) {
                    printRect.Size = resizedRect;
                }

            }

            // prepare timestamp
            float dateStringWidth = 0;
            float dateStringHeight = 0;
            if (conf.OutputPrintTimestamp) {
                Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
                string dateString = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
                dateStringWidth = e.Graphics.MeasureString(dateString, f).Width;
                dateStringHeight = e.Graphics.MeasureString(dateString, f).Height;
                e.Graphics.DrawString(dateString, f, Brushes.Black, pageRect.Width / 2 - (dateStringWidth / 2), pageRect.Height - dateStringHeight);
            }

            // align the image
            printRect = ScaleHelper.GetAlignedRectangle(printRect, new RectangleF(0, 0, pageRect.Width, pageRect.Height - dateStringHeight * 2), alignment);

            e.Graphics.DrawImage(image,printRect,imageRect,GraphicsUnit.Pixel);
        }