Der C# Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
public class ReflectedThumbnail : IDisposable
{
public Bitmap ThumbnailImage;
public int ImageBottom;
public Size MaxSize;
public int Skew;
public int FrameWidth;
public int MaxReflectionLength;
public Pen BorderPen = Pens.Gray;
public Brush FrameBrush = Brushes.White;
private struct Pixel
{
public byte B;
public byte G;
public byte R;
public byte A;
}
public ReflectedThumbnail() : this(
new Size( 128, 128 ),
20,
3,
80 )
{
}
public ReflectedThumbnail(
Size maxSize,
int skew,
int frameWidth,
int maxReflectionLength )
{
MaxSize = maxSize;
Skew = skew;
FrameWidth = frameWidth;
MaxReflectionLength = maxReflectionLength;
}
public void Dispose()
{
DisposeBitmap();
}
public void DisposeBitmap()
{
if ( ThumbnailImage != null )
{
ThumbnailImage.Dispose();
ThumbnailImage = null;
}
}
public void create(
Bitmap image )
{
DisposeBitmap();
Size sz = adaptProportionalSize(
new Size( MaxSize.Width - FrameWidth * 2, MaxSize.Height - FrameWidth * 2 ),
image.Size );
sz.Width += FrameWidth * 2;
sz.Height += FrameWidth * 2;
ImageBottom = sz.Height;
int reflectionLength = Math.Min( sz.Height, MaxReflectionLength );
ThumbnailImage = new Bitmap(
sz.Width,
sz.Height + reflectionLength + Skew );
using ( Bitmap
bmpFramed = createFramedBitmap( image, sz ),
bmpReflection = createReflectedBitmap( bmpFramed, reflectionLength ) )
using ( Graphics g = Graphics.FromImage( ThumbnailImage ) )
{
// draw the reflected image to the resulting image
// using a shear transform
System.Drawing.Drawing2D.Matrix m = g.Transform;
m.Shear( 0, (float)Skew / sz.Width );
m.Translate( 0, sz.Height - Skew - 1 );
g.Transform = m;
g.DrawImage( bmpReflection, Point.Empty );
g.ResetTransform();
// draw the real (framed) image to the resulting image
// one column at a time, slightly altering the height
// of the destination column in order to create the
// "half sheared" transform
for ( int x = 0 ; x < sz.Width ; x++ )
g.DrawImage(
bmpFramed,
new RectangleF( x, 0, 1, sz.Height - Skew * (float)(sz.Width - x) / _
sz.Width ),
new RectangleF( x, 0, 1, sz.Height ),
GraphicsUnit.Pixel );
}
}
protected virtual Bitmap createFramedBitmap( Bitmap bmpSource, Size szFull )
{
Bitmap bmp = new Bitmap( szFull.Width, szFull.Height );
using ( Graphics g = Graphics.FromImage( bmp ) )
{
g.FillRectangle( FrameBrush, 0, 0, szFull.Width, szFull.Height );
g.DrawRectangle( BorderPen, 0, 0, szFull.Width - 1, szFull.Height - 1 );
g.InterpolationMode = _
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(
bmpSource,
new Rectangle( FrameWidth, FrameWidth, szFull.Width - FrameWidth * 2, _
szFull.Height - FrameWidth * 2 ),
new Rectangle( Point.Empty, bmpSource.Size ),
GraphicsUnit.Pixel );
}
return bmp;
}
private static unsafe byte gaussBlur( byte* p, int width )
{
unchecked
{
return (byte)(( p[-width - 4] + 2 * p[-width] + p[-width + 4] +
2 * p[-4] + 4 * p[0] + 2 * p[4] +
p[width - 4] + 2 * p[width] + p[width + 4] ) / 16);
}
} 0 |