Create Full Color Bitmap

image_pdfimage_print

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

public class CreateFullColorBitmap : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new CreateFullColorBitmap());
}
public CreateFullColorBitmap()
{
int[] array = new int[256 * 256];

for (int x = 0; x < 256; x++) for (int y = 0; y < 256; y++) { int b = x; int g = 0; int r = y; array[256 * y + x] = b | (g << 8) | (r << 16); } BitmapSource bitmap= BitmapSource.Create(256, 256, 96, 96, PixelFormats.Bgr32, null, array, 256 * 4); Image img = new Image(); img.Source = bitmap; Content = img; } } [/csharp]

Draw Graphics On Bitmap

image_pdfimage_print
   
 

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

    public class DrawGraphicsOnBitmap : Window
    {
        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            app.Run(new DrawGraphicsOnBitmap());
        }
        public DrawGraphicsOnBitmap()
        {
            Background = Brushes.Khaki;
            RenderTargetBitmap renderbitmap = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Default);
            DrawingVisual drawvis = new DrawingVisual();
            DrawingContext dc = drawvis.RenderOpen();
            dc.DrawRoundedRectangle(Brushes.Blue, new Pen(Brushes.Red, 10),new Rect(25, 25, 50, 50), 10, 10);
            dc.Close();
            renderbitmap.Render(drawvis);
            Image img = new Image();
            img.Source = renderbitmap;

            Content = img;
        }
    }

   
     


Draw Buttons On Bitmap

image_pdfimage_print

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

public class DrawButtonsOnBitmap : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new DrawButtonsOnBitmap());
}
public DrawButtonsOnBitmap()
{
UniformGrid unigrid = new UniformGrid();
unigrid.Columns = 4;

for (int i = 0; i < 32; i++) { ToggleButton btn = new ToggleButton(); btn.Width = 96; btn.Height = 24; btn.IsChecked = true; unigrid.Children.Add(btn); } unigrid.Measure(new Size(Double.PositiveInfinity,Double.PositiveInfinity)); Size szGrid = unigrid.DesiredSize; unigrid.Arrange(new Rect(new Point(0, 0), szGrid)); RenderTargetBitmap renderbitmap = new RenderTargetBitmap((int)Math.Ceiling(szGrid.Width), (int)Math.Ceiling(szGrid.Height), 96, 96, PixelFormats.Default); renderbitmap.Render(unigrid); Image img = new Image(); img.Source = renderbitmap; Content = img; } } [/csharp]

Create Indexed Bitmap

image_pdfimage_print

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

public class CreateIndexedBitmap : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new CreateIndexedBitmap());
}
public CreateIndexedBitmap()
{
List colors = new List();

for (int r = 0; r < 256; r += 17) for (int b = 0; b < 256; b += 17) colors.Add(Color.FromRgb((byte)r, 0, (byte)b)); BitmapPalette palette = new BitmapPalette(colors); byte[] array = new byte[256 * 256]; for (int x = 0; x < 256; x++) for (int y = 0; y < 256; y++) array[256 * y + x] = (byte)(((int)Math.Round(y / 17.0) << 4) | (int)Math.Round(x / 17.0)); BitmapSource bitmap = BitmapSource.Create(256, 256, 96, 96, PixelFormats.Indexed8,palette, array, 256); Image img = new Image(); img.Source = bitmap; Content = img; } } [/csharp]

Simple Border

image_pdfimage_print


   
     

<Window x:Class="Content.SimpleBorder"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SimpleBorder" Height="300" Width="300">
  <Border Margin="5" Padding="5" Background="LightYellow" 
          BorderBrush="SteelBlue" BorderThickness="3,5,3,5" CornerRadius="3"
          VerticalAlignment="Top">
    <StackPanel>
      <Button Margin="3">One</Button>
      <Button Margin="3">Two</Button>
      <Button Margin="3">Three</Button>
    </StackPanel>
  </Border>
</Window>

   
    
    
    
    
     


Set Border's BorderBrush to ImageBrush

image_pdfimage_print


   
     
<Page  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   x:Class="Microsoft.Samples.Graphics.UsingImageBrush.TilingExample" >

  <Grid Margin="20">
    <Border Grid.Row="6" Grid.Column="4" BorderThickness="20" Width="200"
     HorizontalAlignment="Left">
      <Border.BorderBrush>
        <ImageBrush ImageSource="c:image.jpg" Viewport="-0.1,-0.1,1.5,1.5" />
      </Border.BorderBrush>
      <DockPanel>
        <TextBlock DockPanel.Dock="Top" TextWrapping="Wrap" Margin="10">This DockPanel has a border painted with an ImageBrush.</TextBlock>
      </DockPanel>
    </Border>

  </Grid>
</Page>

   
    
    
    
    
     


Set Border Margin, BorderThickness, BorderBrush, Width and Height

image_pdfimage_print


   
     

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="160" Width="300">
    <Grid>
        <Border Margin="8" 
                BorderThickness="1"
                BorderBrush="Black"
                Width="160" 
                Height="60">
            <TextBlock Foreground="DarkGray" 
                       VerticalAlignment="Center" 
                       HorizontalAlignment="Center"
                       ToolTip="This is a custom tooltip"
                       Text="Mouse Over for tooltip"/>
        </Border>
    </Grid>
    
</Window>