code_junkie

Setting WPF image source in code

Nov 12th, 2011
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. Assembly asm = Assembly.GetExecutingAssembly();
  2. Stream iconStream = asm.GetManifestResourceStream("SomeImage.png");
  3. PngBitmapDecoder iconDecoder = new PngBitmapDecoder(iconStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
  4. ImageSource iconSource = iconDecoder.Frames[0];
  5. _icon.Source = iconSource;
  6.  
  7. Image finalImage = new Image();
  8. finalImage.Width = 80;
  9. ...
  10. BitmapImage logo = new BitmapImage()
  11. logo.BeginInit();
  12. logo.UriSource = new Uri("pack://application:,,,/ApplicationName;component/Resources/logo.png");
  13. logo.EndInit();
  14. ...
  15. finalImage.Source = logo;
  16.  
  17. var uriSource = new Uri(@"/WpfApplication1;component/Images/Untitled.png", UriKind.Relative);
  18. foo.Source = new BitmapImage(uriSource);
  19.  
  20. string packUri = "pack://application:,,,/AssemblyName;component/Images/icon.png";
  21. _image.Source = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource;
  22.  
  23. Assembly asm = Assembly.GetExecutingAssembly();
  24. Stream iconStream = asm.GetManifestResourceStream("SomeImage.png");
  25. BitmapImage bitmap = new BitmapImage();
  26. bitmap.BeginInit();
  27. bitmap.StreamSource = iconStream;
  28. bitmap.EndInit();
  29. _icon.Source = bitmap;
  30.  
  31. if (File.Exists(imagePath))
  32. {
  33. // Create image element to set as icon on the menu element
  34. Image icon = new Image();
  35. BitmapImage bmImage = new BitmapImage();
  36. bmImage.BeginInit();
  37. bmImage.UriSource = new Uri(imagePath, UriKind.Absolute);
  38. bmImage.EndInit();
  39. icon.Source = bmImage;
  40. icon.MaxWidth = 25;
  41. item.Icon = icon;
  42. }
  43.  
  44. <MenuItem Name="mnuFileSave" Header="Save" Command="ApplicationCommands.Save">
  45. <MenuItem.Icon>
  46. <Label VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="Wingdings"><</Label>
  47. </MenuItem.Icon>
  48. </MenuItem>
  49.  
  50. VisualBrush brush = new VisualBrush { TileMode = TileMode.None };
  51.  
  52.  
  53. brush.Visual = frame;
  54.  
  55. brush.AlignmentX = AlignmentX.Center;
  56. brush.AlignmentY = AlignmentY.Center;
  57. brush.Stretch = Stretch.Uniform;
  58.  
  59. GeometryDrawing drawing = new GeometryDrawing();
  60.  
  61. drawing.Brush = brush;
  62.  
  63. //Brush this in 1, 1 ratio
  64. RectangleGeometry rect = new RectangleGeometry { Rect = new Rect(0, 0, 1, 1) };
  65. drawing.Geometry = rect;
  66.  
  67. new DrawingImage(drawing);
  68.  
  69. <Image>
  70. <Image.Source>
  71. <BitmapImage UriSource="/yourassembly;component/YourImage.PNG"></BitmapImage>
  72. </Image.Source>
  73. </Image>
  74.  
  75. Properties.Resources.ResourceManager.GetURI("my_image");
  76.  
  77. // Convert the image in resources to a Stream
  78. Stream ms = new MemoryStream()
  79. Properties.Resources.MyImage.Save(ms,ImageFormat.Png);
  80.  
  81. // Create a BitmapImage with the Stream.
  82. BitmapImage bitmap = new BitmapImage();
  83. bitmap.BeginInit();
  84. bitmap.StreamSource = ms;
  85. bitmap.EndInit();
  86.  
  87. // Set as Source
  88. Source = bitmap;
  89.  
  90. this.Icon = new BitmapImage(new Uri("Icon.ico", UriKind.Relative));
  91.  
  92. <UserControl.Resources>
  93. <ResourceDictionary>
  94. <ImageBrush x:Key="PosterBrush" ImageSource="..ResourcesImagesEmptyPoster.jpg" Stretch="UniformToFill"/>
  95.  
  96. </ResourceDictionary>
  97. </UserControl.Resources>
  98.  
  99. ImageBrush posterBrush = (ImageBrush)Resources["PosterBrush"];
  100.  
  101. <Button Width="200" Height="70">
  102. <Button.Content>
  103. <StackPanel>
  104. <Image Width="20" Height="20">
  105. <Image.Source>
  106. <BitmapImage UriSource="/Company.ProductAssembly;component/Icons/ClickMe.png"></BitmapImage>
  107. </Image.Source>
  108. </Image>
  109. <TextBlock HorizontalAlignment="Center">Click me!</TextBlock>
  110. </StackPanel>
  111. </Button.Content>
  112. </Button>
  113.  
  114. static ImageSource PngStreamToImageSource (Stream pngStream) {
  115. var decoder = new PngBitmapDecoder(pngStream,
  116. BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
  117. return decoder.Frames[0];
  118. }
  119.  
  120. Uri iconUri = new Uri("pack://application:,,,/ImageNAme.ico", UriKind.RelativeOrAbsolute);
  121. NotifyIcon.Icon = BitmapFrame.Create(iconUri);
Add Comment
Please, Sign In to add comment