BitmapSource bs = null, bs2 = null, bs3 = null, bs4 = null;
BitmapData bitmapData = new BitmapData(), bitmapData2 = new BitmapData(), bitmapData3 = new BitmapData(), bitmapData4 = new BitmapData();
int imageWidth = bitmap.Width / 3;
int imageHeight =bitmap.Height;
var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
var size = (rect.Width * rect.Height) * 4;
try
{
bitmapData = bitmap.LockBits(
rect,
ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
bitmapData2 = bitmap2.LockBits(
rect,
ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
bitmapData3 = bitmap3.LockBits(
rect,
ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
bs = BitmapSource.Create(
bitmap.Width,
bitmap.Height,
bitmap.HorizontalResolution,
bitmap.VerticalResolution,
PixelFormats.Bgra32,
null,
bitmapData.Scan0,
size,
bitmapData.Stride);
bs2 = BitmapSource.Create(
bitmap2.Width,
bitmap2.Height,
bitmap2.HorizontalResolution,
bitmap2.VerticalResolution,
PixelFormats.Bgra32,
null,
bitmapData2.Scan0,
size,
bitmapData2.Stride);
bs3 = BitmapSource.Create(
bitmap3.Width,
bitmap3.Height,
bitmap3.HorizontalResolution,
bitmap3.VerticalResolution,
PixelFormats.Bgra32,
null,
bitmapData3.Scan0,
size,
bitmapData3.Stride);
}
finally
{
bitmap.UnlockBits(bitmapData);
bitmap2.UnlockBits(bitmapData2);
bitmap3.UnlockBits(bitmapData3);
}
//imageWidth = bitmap.Width / 2;
//imageHeight = bitmap.Height / 2;
// Draws the images into a DrawingVisual component
System.Windows.Size size1 = new System.Windows.Size(imageWidth * 2, imageHeight * 2);
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
drawingContext.DrawImage(bs, new Rect(0, 0, imageWidth, imageHeight));
drawingContext.DrawImage(bs2, new Rect(imageWidth, 0, imageWidth, imageHeight));
drawingContext.DrawImage(bs3, new Rect(imageWidth * 2, 0, imageWidth, imageHeight));
//drawingContext.DrawImage(bs3, new Rect(0, imageHeight, imageWidth, imageHeight));
//drawingContext.DrawRectangle(new VisualBrush(), null, new Rect(new System.Windows.Point(), size1));
}
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
// Converts the Visual (DrawingVisual) into a BitmapSource
RenderTargetBitmap bmp = new RenderTargetBitmap(imageWidth*3, imageHeight, 100,100, PixelFormats.Default);//PixelFormats.Pbgra32
//RenderTargetBitmap bmp = new RenderTargetBitmap(imageWidth * 3, imageHeight, 110, 110, PixelFormats.Pbgra32);//PixelFormats.Pbgra32
bmp.Render(drawingVisual);
try
{
encoder.Frames.Add(BitmapFrame.Create(bmp));
// Saves the image into a file using the encoder
using (Stream stream = File.Create(Path.Combine(ApplicationCache.Instance().DefaultImageFolder, fileName + Constants.ImageCapture.DEFAULT_IMAGE_EXTENSION)))
{
encoder.Save(stream);
}
GC.Collect();
}
catch (Exception EX)
{
MessageBox.Show(EX.Message);
}