Adding Image to FixedPage in WPF

前端 未结 2 1032
北海茫月
北海茫月 2021-01-24 04:19

I want to be able to print images with other UIElements. I have a FixedPage instance and trying to add image like so

// Basic printing stuff
var printDialog = ne         


        
相关标签:
2条回答
  • 2021-01-24 04:49

    First i would like to suggest that, see below.

    Set the sourceStream of the Image for BitMap
        bitImage.StreamSource = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    

    Then freeze the BitMap Image, or it ll not be in the instance as render able image.

    bitImage.StreamSource.Seek(0, System.IO.SeekOrigin.Begin);
    bitImage.Freeze();
    

    This should work, if not i personally think it is a bad idea to directly work with BitMaps, i use Bitmap to create image form file then copy it to the type Image(system.drawing iguess), something like below

    var tempImage = new Image {Source = bitImage};
    var imageObject = new ImageObject(tempImage, fileName);
    bitImage.StreamSource.Dispose();
    

    this tempImage can be added to ur page as regular UIElement. Hope it helps.

    0 讨论(0)
  • 2021-01-24 04:56

    To be more clear on my Code

    var bitImage = new BitmapImage();
    bitImage.BeginInit();
    bitImage.StreamSource = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    bitImage.DecodePixelWidth = 250;
    bitImage.CacheOption = BitmapCacheOption.OnLoad;
    bitImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
    bitImage.EndInit();
    bitImage.StreamSource.Seek(0, System.IO.SeekOrigin.Begin);
    bitImage.Freeze();
    
    var tempImage = new Image {Source = bitImage};
    var imageObject = new ImageObject(tempImage, fileName);
    bitImage.StreamSource.Dispose();
    page.Children.Add(imageObject);
    
    0 讨论(0)
提交回复
热议问题