Rank: Advanced Member
Groups: Member, Administration Joined: 8/2/2003(UTC) Posts: 876
Thanks: 2 times Was thanked: 27 time(s) in 27 post(s)
|
Unfortunately we have not included C# samples to the documentation. We tried to make the documentation as comprehensive as possible, but if you find something to be unclear, please feel free to post here or submit case). Meanwhile, the same code sample ported to C#: Code:<%@ Page Language="C#" %>
<%@ import Namespace="GraphicsMill" %>
<%
// Declarations
GraphicsMill.Bitmap bitmap = new GraphicsMill.Bitmap();
GraphicsMill.Bitmap background = new GraphicsMill.Bitmap();
GraphicsMill.Bitmap mask = new GraphicsMill.Bitmap();
// Offset coordinates of the bitmap on background
int x, y;
// Filenames
string bitmapFilename = Server.MapPath("FrontImage.jpg");
string backgroundFilename = Server.MapPath("BackgroundImage.jpg");
string mergedFilename = Server.MapPath("Result.jpg");
string maskFilename = Server.MapPath("Mask.tif");
// Load bitmaps
background.LoadFromFile(backgroundFilename);
bitmap.LoadFromFile(bitmapFilename);
mask.LoadFromFile(maskFilename);
// Check if alpha channel exists in the image. If not,
// we must add it
if (!bitmap.Data.HasAlphaChannel)
{
bitmap.Channels.AddAlpha();
}
// In this sample we will put the image into the center.
// To centrize the coordinates, make these calculations:
x = (background.Data.Width - bitmap.Data.Width) / 2;
y = (background.Data.Height - bitmap.Data.Height) / 2;
// Alpha channel is always 8-bit grayscale image. That's why we need
// to convert our 1-bit mask to grayscale.
mask.Data.ConvertTo8bppGrayscale(false, GraphicsMillConstants.ColorWhite);
// If your mask contains black as opaque area, and white as transparent,
// you must invert it. By the way, if you always use 1-bit images, the
// optimal way to invert the image is to swap palette entries before converting
// to grayscale instead of using Invert method.
mask.ColorAdjustment.Invert();
// To be able to retrieve mask to alpha channel of the image, we
// need make sure that it has the same dimensions as target image.
// You can also use Resize instead of Crop if necessary.
mask.Transforms.Crop (0, 0, bitmap.Data.Width, bitmap.Data.Height, GraphicsMillConstants.ColorWhite);
// This method replaces channel with given number (zero-based) to the given
// grayscale image. Number 3 means alpha in ARGB pixel format.
bitmap.Channels.Replace(mask, 3);
// Apply alpha blending
bitmap.DrawOnBitmap(background, x, y, -1, -1, 0, 0, -1, -1, CombineMode.CombineModeAlpha, 255, InterpolationMode.InterpolationModeMediumQuality, true, true, true);
// Save the result
background.SaveToFile(mergedFilename);
Response.ContentType = "image/jpeg";
Response.BinaryWrite((byte[])background.SaveToMemory());
%>
Edited by user Friday, May 23, 2008 4:11:14 PM(UTC)
| Reason: Not specified
|