.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Saving problem of DrawingGroup Image. This container shows Metadata as null
 

Saving problem of DrawingGroup Image. This container shows Metadata as null

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

  • Edited bymadhav_1 Tuesday, September 22, 2009 1:08 PMcorrection
  • Edited bymadhav_1 Wednesday, September 23, 2009 9:47 AMCorrection
  •  
madhav_1
I don't see where you set mImage.Source, but I'm guessing you set it to drawingImageSource. DrawingImage is not a BitmapSource; it is an ImageSource. In other words, you can't just encode a DrawingImage because it has no bits. If you want to save it you'll need to render it to a bitmap with RenderTargetBitmap. Search the forum and you'll find lots of RTB examples.

If all you're trying to do is resave a bitmap, you don't need do do the whole Drawing thing of course but I'm guessing that's not what you really want to do and instead were just simplifying things for the forum.
  • Marked As Answer bymadhav_1 Wednesday, September 23, 2009 9:50 AM
  •  
Jordan Parker - MSFT
I don't see where you set mImage.Source, but I'm guessing you set it to drawingImageSource. DrawingImage is not a BitmapSource; it is an ImageSource. In other words, you can't just encode a DrawingImage because it has no bits. If you want to save it you'll need to render it to a bitmap with RenderTargetBitmap. Search the forum and you'll find lots of RTB examples.

If all you're trying to do is resave a bitmap, you don't need do do the whole Drawing thing of course but I'm guessing that's not what you really want to do and instead were just simplifying things for the forum.
  • Marked As Answer bymadhav_1 Wednesday, September 23, 2009 9:50 AM
  •  
Jordan Parker - MSFT
Hi Jordan Parker,

Thank you for your timely reply,

Mistakenly the mImage is removed, Sorry it is corrected now.

Thanks once again for reply.

madhav_1

You can use google to search for other answers

Custom Search

More Threads

• Syntax of Geometry property value
• ListView Cell
• Performance Issue: MediaElement
• How to read/insert tags to any file? And how to display associated icon of the file in my list?
• Custom List: Use BindingList or ObservableCollection
• Good resources for advanced control authoring - where?
• Binding TextBox.Text to Nullables
• MVVM binding best practice
• CreateGraphics, Image, WPF, C#
• Databinding Syntax?