FormatConvertedBitmap DestinationFormat=Gray4


   
  


<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ImageElementExample.FormatConvertedExample"
    Title="FormatConverted Example"
    Loaded="PageLoaded">
   <Page.Resources>
      <BitmapImage x:Key="masterImage" UriSource="c:image.jpg" />
   </Page.Resources>
   <DockPanel>
      <Image Width="200" Source="{StaticResource masterImage}" />
      <Grid Name="convertedGrid" DockPanel.Dock="Top">
         <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
         </Grid.ColumnDefinitions>
         <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
         </Grid.RowDefinitions>
         <Image Width="200" Grid.Column="0" Grid.Row="1">
            <Image.Source>
               <FormatConvertedBitmap Source="{StaticResource masterImage}"  DestinationFormat="Gray4" />
            </Image.Source>
         </Image>
      </Grid>
   </DockPanel>
</Page>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ImageElementExample
{
   public partial class FormatConvertedExample : Page
   {
      public FormatConvertedExample()
      {
      }
      public void PageLoaded(object sender, RoutedEventArgs args)
      {
         Image grayImage = new Image();
         grayImage.Width = 200;
         grayImage.Margin = new Thickness(5);

         FormatConvertedBitmap fcb = new FormatConvertedBitmap((BitmapImage)this.Resources["masterImage"],PixelFormats.Gray4,null,0);
         grayImage.Source = fcb;

         Grid.SetColumn(grayImage, 2);
         Grid.SetRow(grayImage, 1);
         convertedGrid.Children.Add(grayImage);
      }
   }
}

   
    
     


Load image source from a hard code directory


   
  


<Window x:Class="AssemblyResources.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AssemblyResources" Height="300" Width="300">
    <StackPanel>      
      <Button Click="cmdPlay_Click" Padding="5">Play</Button>
      <Image Name="img" Source="c:image.jpg"></Image>
      <MediaElement Name="Sound" Source="c:sound.wav" LoadedBehavior="Manual"></MediaElement>
    </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Reflection;
using System.Resources;
using System.IO;
using System.Globalization;
using System.Windows.Resources;

namespace AssemblyResources
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void cmdPlay_Click(object sender, RoutedEventArgs e)
        {
            img.Source = new BitmapImage(new Uri("file:///c:/image.jpg", UriKind.Relative));
            Sound.Stop();
            Sound.Play();
        }
    }
}

   
    
     


Add Caption for an Image

   
  
<Window x:Class="BitmapProgramming.AddCaption"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BitmapProgramming" Height="300" Width="300"
    >
  <Image x:Name="imageElement" />
</Window>

//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BitmapProgramming
{
    public partial class AddCaption : System.Windows.Window
    {
        public AddCaption()
        {
            InitializeComponent();

            BitmapImage originalBmp = new BitmapImage();
            originalBmp.BeginInit();
            originalBmp.UriSource = new Uri("http://www.your host.com/a.jpeg");
            originalBmp.DownloadCompleted += delegate{
                Grid rootGrid = new Grid();
                Image img = new Image();
                img.Source = originalBmp;
                rootGrid.Children.Add(img);
                TextBlock caption = new TextBlock();
                caption.Text = "Ian&#039;s car";
                caption.VerticalAlignment = VerticalAlignment.Bottom;
                caption.HorizontalAlignment = HorizontalAlignment.Center;
                caption.Margin = new Thickness(5);
                caption.Padding = new Thickness(5);
                caption.TextAlignment = TextAlignment.Center;
                caption.TextWrapping = TextWrapping.Wrap;
                rootGrid.Children.Add(caption);

                RenderTargetBitmap bmp = new RenderTargetBitmap(originalBmp.PixelWidth, originalBmp.PixelHeight,originalBmp.DpiX, originalBmp.DpiY, PixelFormats.Pbgra32);
                rootGrid.Measure(new Size(originalBmp.Width, originalBmp.Height));
                rootGrid.Arrange(new Rect(0, 0, originalBmp.Width, originalBmp.Height));
                bmp.Render(rootGrid);

                imageElement.Source = bmp;
            };
            originalBmp.EndInit();
        }
    }
}

   
    
     


Modify Image Pixels



//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BitmapProgramming
{
public partial class ModifyPixels : System.Windows.Window
{
public ModifyPixels()
{
InitializeComponent();

BitmapImage originalBmp = new BitmapImage();
originalBmp.BeginInit();
originalBmp.UriSource = new Uri(“http://www.your host.com/a.jpeg”);
originalBmp.DownloadCompleted += delegate{
BitmapSource prgbaSource = new FormatConvertedBitmap(originalBmp,PixelFormats.Pbgra32, null, 0);
WriteableBitmap bmp = new WriteableBitmap(prgbaSource);

int w = 20;
int h = 30;
int[] pixelData = new int[w * h];
int widthInBytes = 4 * w;

bmp.CopyPixels(pixelData, widthInBytes, 0);
for (int i = 0; i < pixelData.Length; ++i) { pixelData[i] ^= 0x00112233; } bmp.WritePixels(new Int32Rect(0, 0, w, h),pixelData, widthInBytes, 0); imageElement.Source = bmp; }; originalBmp.EndInit(); } } } [/csharp]

Load image from a URI

   
  
<Window x:Class="BitmapProgramming.UseBitmapImage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BitmapProgramming" Height="300" Width="300"
    >
</Window>


//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BitmapProgramming
{
    public partial class UseBitmapImage : System.Windows.Window
    {
        public UseBitmapImage()
        {
            InitializeComponent();

            Image imageElement = new Image();
            imageElement.Source = new BitmapImage(new Uri("http://www.your host.com/a.jpg"));

            this.Content = imageElement;

        }

    }
}

   
    
     


Use Render Target Bitmap


   
  
<Window x:Class="BitmapProgramming.UseRenderTargetBitmap"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BitmapProgramming" Height="300" Width="300">
  <Image x:Name="imageElement" />
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BitmapProgramming
{
    public partial class UseRenderTargetBitmap : System.Windows.Window
    {
        public UseRenderTargetBitmap()
        {
            InitializeComponent();
            RenderTargetBitmap bmp = new RenderTargetBitmap(
                300, 150,     // Dimensions in physical pixels
                300, 300,     // Pixel resolution (dpi)
                PixelFormats.Pbgra32);

            Ellipse e = new Ellipse();
            e.Fill = Brushes.Red;
            e.Measure(new Size(96, 48));
            e.Arrange(new Rect(0, 0, 96, 48));

            bmp.Render(e);
            imageElement.Source = bmp;
        }
    }
}

   
    
     


Transformed image example


   
  
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ImageElementExample.TransformedImageExample"
    Title="Transformed Image Example"
    Loaded="PageLoaded">
   <Page.Resources>
      <BitmapImage x:Key="masterImage" UriSource="c:image.jpg"/>
   </Page.Resources>
   <DockPanel>
      <Image Source="{StaticResource masterImage}" Width="150" Margin="5"/>
      <Grid Name="transformedGrid">
         <Image Width="150" Margin="5" Grid.Column="0" Grid.Row="1">
           <Image.Source>
             <TransformedBitmap Source="c:image.jpg" >
               <TransformedBitmap.Transform>
                 <RotateTransform Angle="90"/>
               </TransformedBitmap.Transform>
             </TransformedBitmap>
           </Image.Source>
         </Image>
      </Grid>
   </DockPanel>
</Page>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;


namespace ImageElementExample
{
   public partial class TransformedImageExample : Page
   {
      public TransformedImageExample()
      {
      }

      public void PageLoaded(object sender, RoutedEventArgs args)
      {
         Image rotated90 = new Image();
         rotated90.Width = 150;

         TransformedBitmap tb = new TransformedBitmap();

         BitmapImage bi = new BitmapImage();
         bi.BeginInit();
         bi.UriSource = new Uri(@"file:///c:/image.jpg", UriKind.RelativeOrAbsolute);
         bi.EndInit();

         tb.BeginInit();
         tb.Source = bi;

         RotateTransform transform = new RotateTransform(90);
         tb.Transform = transform;
         tb.EndInit();

         rotated90.Source = tb;

         Grid.SetColumn(rotated90, 1);
         Grid.SetRow(rotated90, 1);
         transformedGrid.Children.Add(rotated90);
      }
   }
}