Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Drawing.Drawing2D;
- public static Bitmap OverlayImage(Image background, Image foreground, Point foregroundLocation, Size foregroundSize, float opacity, bool resizeBackground)
- {
- Bitmap returnValue = null;
- Graphics returnValueGraphics = null;
- int backgroundHeight = background.Height;
- Point backgroundLocation = new Point(0, 0);
- int backgroundWidth = background.Width;
- int foregroundHeight = foreground.Height;
- int foregroundWidth = foreground.Width;
- int newHeight = 0;
- int newWidth = 0;
- if (false == resizeBackground)
- {
- newHeight = backgroundHeight;
- newWidth = backgroundWidth;
- }
- else
- {
- if (foregroundLocation.X < 0)
- {
- backgroundLocation = new Point(Math.Abs(foregroundLocation.X), backgroundLocation.Y);
- foregroundLocation = new Point(0, foregroundLocation.Y);
- }
- if (foregroundLocation.Y < 0)
- {
- backgroundLocation = new Point(backgroundLocation.X, Math.Abs(foregroundLocation.Y));
- foregroundLocation = new Point(foregroundLocation.X, 0);
- }
- newHeight = Math.Max((backgroundLocation.Y + backgroundHeight), (foregroundLocation.Y + Math.Min (foregroundHeight, foregroundSize.Height)));
- newWidth = Math.Max((backgroundLocation.X + backgroundWidth), (foregroundLocation.X + Math.Min (foregroundWidth, foregroundSize.Width)));
- }
- returnValue = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb);
- returnValue.SetResolution(background.HorizontalResolution, background.VerticalResolution);
- returnValueGraphics = Graphics.FromImage(returnValue);
- returnValueGraphics.SmoothingMode = SmoothingMode.AntiAlias;
- returnValueGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
- returnValueGraphics.DrawImage(
- background
- , new Rectangle(backgroundLocation, background.Size)
- , 0
- , 0
- , backgroundWidth
- , backgroundHeight
- , GraphicsUnit.Pixel
- );
- ImageAttributes imageAttributes = new ImageAttributes();
- float[][] colorMatrixElements = {
- new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
- new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
- new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
- new float[] {0.0f, 0.0f, 0.0f, opacity, 0.0f},
- new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
- ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
- imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
- returnValueGraphics.DrawImage(
- foreground
- , new Rectangle(foregroundLocation, foregroundSize)
- , 0
- , 0
- , foreground.Width
- , foreground.Height
- , GraphicsUnit.Pixel
- , imageAttributes
- );
- returnValueGraphics.Dispose();
- return (returnValue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement