Hi Friends,
I appreciate for any positive reply for this pls. I am trying to save a Bitmapsource having image data from System.Windows.Controls.Image which inturn having image data from DrawingGroup. I think the saving proble is due to the null meta data of BitmapSource which is obtaining the Image from
the DrawingGroup. Why this Drawinggroup is not accompanying the meta data. Is there any alternative method for adding group of image in desired order.
Here is the code
string FilesPath = @"E:\OnTest\";
DrawingGroup imageDrawingsGroup = new DrawingGroup();
System.Windows.Controls.
Image mImage = new System.Windows.Controls.Image();
Stream imageStreamSource = null;
TiffBitmapDecoder decoder = null;
using (DrawingContext dc = imageDrawingsGroup.Open())
{
//Load image
imageStreamSource =new FileStream(FilesPath.Trim() + "tiff1.tif", FileMode.Open, FileAccess.Read, FileShare.Read);
//decode
decoder =new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
//define rectangle
ImageDrawing imgDraw1 = new ImageDrawing();
imgDraw1.Rect =new System.Windows.Rect(150, 0, 25, 25);
//set image to the rect
BitmapSource bitmapSource1 = decoder.Frames[0];
//add the rectangle image to the Drawing Group
imageDrawingsGroup.Children.Add(imgDraw1);
//close stream
imageStreamSource.Close();
//Set the image group to drawing
DrawingImage drawingImageSource = new DrawingImage(imageDrawingsGroup);
// Freeze the DrawingImage for performance benefits.
drawingImageSource.Freeze();
//set the drawing image to an System.Windows.Controls.Image
mImage.Stretch =Stretch.None;
//Set Image
mImage.Source=drawingImageSource;
//pop context
dc.close();
}
//Save the Image as BitmapSource
try
{
//-----------------------------------------------------------------------------------------------
// The problem detected here...... mImage accompanied null bitmap data what may be the reason?.
Offcourse the problem from the Drawing group. Other than Drawing group anyother grouping method in System.Windows.Media namespace please.
//-----------------------------------------------------------------------------------------------
System.Windows.Media.Imaging.BitmapSource bmsource = mImage.Source as BitmapSource;
FileStream stream = new FileStream(@"E:\TiffStore\Main.tif", FileMode.Create);
TiffBitmapEncoder encoder = new TiffBitmapEncoder();
TextBlock myTextBlock = new TextBlock();
myTextBlock.Text ="Codec Author is: " + encoder.CodecInfo.Author.ToString();
encoder.Compression =
TiffCompressOption.Default;
encoder.Frames.Add(BitmapFrame.Create(bmsource));
encoder.Save(stream);
}
catch (Exception ex)
{
throw new Exception(ex);
}
Thanks & Regards,
Madhav