Tìm kiếm Blog này

Thứ Ba, 31 tháng 7, 2012

A Hatch Drawing Brush for WPF in Visual

The original post in VB.NET is here

We've now come to the conclusion that different groups of people are in charge of developing different technologies over at Microsoft. The WPF (Windows Presentation Foundation) development team made a significant number of improvements over Windows Forms and GDI+, but in the process missed a couple of things.
One very important item is the System.Drawing.HatchBrush from System.Drawing.Drawing2D which was very useful in Windows Forms but is now totally absent in WPF. We have made an interim replacement, which although does not use the win32 API directly should provide a remedy for most situations.
If used conservatively it should not have that much of an impact on the performance of your WPF applications.
WPF does not use the OnPaint override like GDI+. Instead you have to override the OnRender method. One important thing to note however is that WPF will disregard any drawing made to the DrawingContext unless the background mode is set to transparent, so the XAML for the Window should look like this:
<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="404" Width="727" Background="Transparent">
</Window>
public partial class Window1: Window
    {

        private DrawingContext mp_oGraphics;

        public FillPattern()
        {
            InitializeComponent();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            mp_oGraphics = drawingContext;
            mp_oGraphics.DrawRectangle(Brushes.DarkGray, null, new Rect(new Point(0, 0), new Point(this.Width, this.Height)));
            int i;
            int j;
            j = 0;
            for (i = 0; i <= 9; i++)
            {
                HatchFill(0, j * 30, 100, (j * 30) + 20, Colors.Black, Colors.White, (HatchEnum.HatchStyle)(HatchEnum.HatchStyle)i);
                j = j + 1;
            }
            j = 0;
            for (i = 10; i <= 19; i++)
            {
                HatchFill(120, j * 30, 220, (j * 30) + 20, Colors.Black, Colors.White, (HatchEnum.HatchStyle)i);
                j = j + 1;
            }

            j = 0;
            for (i = 20; i <= 29; i++)
            {
                HatchFill(240, j * 30, 340, (j * 30) + 20, Colors.Black, Colors.White, (HatchEnum.HatchStyle)i);
                j = j + 1;
            }
            j = 0;
            for (i = 30; i <= 39; i++)
            {
                HatchFill(360, j * 30, 460, (j * 30) + 20, Colors.Black, Colors.White, (HatchEnum.HatchStyle)i);
                j = j + 1;
            }
            j = 0;
            for (i = 40; i <= 49; i++)
            {
                HatchFill(480, j * 30, 580, (j * 30) + 20, Colors.Black, Colors.White, (HatchEnum.HatchStyle)i);
                j = j + 1;
            }
            j = 0;
            for (i = 50; i <= 52; i++)
            {
                HatchFill(600, j * 30, 700, (j * 30) + 20, Colors.Black, Colors.White, (HatchEnum.HatchStyle)i);
                j = j + 1;
            }
        }

        public void HatchFill(int v_X1, int v_Y1, int v_X2, int v_Y2, Color clrForeColor, Color clrBackColor, HatchEnum.HatchStyle yHatchStyle)
        {
            DrawingBrush mp_ucBrush;
            mp_ucBrush = GetHatchBrush(yHatchStyle, clrForeColor, clrBackColor);
            int iBuff = 0;
            if ((v_X2 - v_X1) <= 0)
            {
                iBuff = v_X1;
                v_X1 = v_X2;
                v_X2 = iBuff;
            }
            if ((v_Y2 - v_Y1) <= 0)
            {
                iBuff = v_Y1;
                v_Y1 = v_Y2;
                v_Y2 = iBuff;
            }
            mp_oGraphics.DrawRectangle(mp_ucBrush, null, new Rect(v_X1, v_Y1, v_X2 - v_X1 + 1, v_Y2 - v_Y1 + 1));
        }


        private DrawingBrush GetHatchBrush(HatchEnum.HatchStyle yHatchStyle, Color clrForeColor, Color clrBackColor)
        {
            DrawingBrush oReturnBrush = new DrawingBrush();
            GeometryGroup oHatchGroup = new GeometryGroup();
            GeometryGroup oHatchCtrlGroup = new GeometryGroup();
            int lWidth = 0;
            int lHeight = 0;
            HatchEnum.HatchType yType = HatchEnum.HatchType.HT_LINE;
            bool bAliased = true;
            switch (yHatchStyle)
            {
                case HatchEnum.HatchStyle.HS_HORIZONTAL:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GLine(0, 0, 7, 0));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_VERTICAL:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GLine(0, 0, 0, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_FORWARDDIAGONAL:
                    lWidth = 16;
                    lHeight = 16;
                    oHatchGroup.Children.Add(mp_GLine(0, 12, 3, 15));
                    oHatchGroup.Children.Add(mp_GLine(0, 4, 11, 15));
                    oHatchGroup.Children.Add(mp_GLine(4, 0, 15, 11));
                    oHatchGroup.Children.Add(mp_GLine(12, 0, 15, 3));
                    System.Diagnostics.Debug.Write(oHatchGroup);
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_BACKWARDDIAGONAL:
                    lWidth = 16;
                    lHeight = 16;
                    oHatchGroup.Children.Add(mp_GLine(0, 12, 3, 15));
                    oHatchGroup.Children.Add(mp_GLine(0, 4, 11, 15));
                    oHatchGroup.Children.Add(mp_GLine(4, 0, 15, 11));
                    oHatchGroup.Children.Add(mp_GLine(12, 0, 15, 3));
                    oHatchGroup.Transform = new RotateTransform(90, 8, 8);
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_LARGEGRID:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GLine(0, 0, 7, 0));
                    oHatchGroup.Children.Add(mp_GLine(0, 0, 0, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_DIAGONALCROSS:
                    lWidth = 7;
                    lHeight = 7;
                    oHatchGroup.Children.Add(mp_GRect(1, 1, 5, 5));
                    oHatchGroup.Transform = new RotateTransform(45, 3, 3);
                    yType = HatchEnum.HatchType.HT_LINE; break;
                    bAliased = false;
                case HatchEnum.HatchStyle.HS_PERCENT05:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(0, 4));
                    oHatchGroup.Children.Add(mp_GPoint(4, 0));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_PERCENT10:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(0, 2));
                    oHatchGroup.Children.Add(mp_GPoint(4, 0));
                    oHatchGroup.Children.Add(mp_GPoint(0, 6));
                    oHatchGroup.Children.Add(mp_GPoint(4, 4));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_PERCENT20:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchGroup.Children.Add(mp_GPoint(2, 2));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_PERCENT25:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchGroup.Children.Add(mp_GPoint(0, 2));
                    oHatchGroup.Children.Add(mp_GPoint(2, 1));
                    oHatchGroup.Children.Add(mp_GPoint(2, 3));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_PERCENT30:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchGroup.Children.Add(mp_GPoint(1, 1));
                    oHatchGroup.Children.Add(mp_GPoint(2, 2));
                    oHatchGroup.Children.Add(mp_GPoint(3, 3));
                    oHatchGroup.Children.Add(mp_GPoint(2, 0));
                    oHatchGroup.Children.Add(mp_GPoint(0, 2));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_PERCENT40:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchGroup.Children.Add(mp_GPoint(2, 0));
                    oHatchGroup.Children.Add(mp_GPoint(4, 0));
                    oHatchGroup.Children.Add(mp_GPoint(6, 0));

                    oHatchGroup.Children.Add(mp_GPoint(3, 1));
                    oHatchGroup.Children.Add(mp_GPoint(5, 1));
                    oHatchGroup.Children.Add(mp_GPoint(7, 1));

                    oHatchGroup.Children.Add(mp_GPoint(0, 2));
                    oHatchGroup.Children.Add(mp_GPoint(2, 2));
                    oHatchGroup.Children.Add(mp_GPoint(4, 2));
                    oHatchGroup.Children.Add(mp_GPoint(6, 2));

                    oHatchGroup.Children.Add(mp_GPoint(1, 3));
                    oHatchGroup.Children.Add(mp_GPoint(3, 3));
                    oHatchGroup.Children.Add(mp_GPoint(5, 3));
                    oHatchGroup.Children.Add(mp_GPoint(7, 3));

                    oHatchGroup.Children.Add(mp_GPoint(0, 4));
                    oHatchGroup.Children.Add(mp_GPoint(2, 4));
                    oHatchGroup.Children.Add(mp_GPoint(4, 4));
                    oHatchGroup.Children.Add(mp_GPoint(6, 4));
                    oHatchGroup.Children.Add(mp_GPoint(1, 5));
                    oHatchGroup.Children.Add(mp_GPoint(3, 5));
                    oHatchGroup.Children.Add(mp_GPoint(7, 5));
                    oHatchGroup.Children.Add(mp_GPoint(0, 6));
                    oHatchGroup.Children.Add(mp_GPoint(2, 6));
                    oHatchGroup.Children.Add(mp_GPoint(4, 6));
                    oHatchGroup.Children.Add(mp_GPoint(6, 6));
                    oHatchGroup.Children.Add(mp_GPoint(1, 7));
                    oHatchGroup.Children.Add(mp_GPoint(3, 7));
                    oHatchGroup.Children.Add(mp_GPoint(5, 7));
                    oHatchGroup.Children.Add(mp_GPoint(7, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_PERCENT50:
                    lWidth = 2;
                    lHeight = 2;
                    oHatchGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchGroup.Children.Add(mp_GPoint(1, 1));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_PERCENT60:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchGroup.Children.Add(mp_GPoint(2, 0));
                    oHatchGroup.Children.Add(mp_GPoint(3, 0));
                    oHatchGroup.Children.Add(mp_GPoint(1, 1));
                    oHatchGroup.Children.Add(mp_GPoint(3, 1));
                    oHatchGroup.Children.Add(mp_GPoint(0, 2));
                    oHatchGroup.Children.Add(mp_GPoint(1, 2));
                    oHatchGroup.Children.Add(mp_GPoint(2, 2));
                    oHatchGroup.Children.Add(mp_GPoint(1, 3));
                    oHatchGroup.Children.Add(mp_GPoint(3, 3));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_PERCENT70:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GPoint(1, 0));
                    oHatchGroup.Children.Add(mp_GPoint(2, 0));
                    oHatchGroup.Children.Add(mp_GPoint(3, 0));
                    oHatchGroup.Children.Add(mp_GPoint(0, 1));
                    oHatchGroup.Children.Add(mp_GPoint(1, 1));
                    oHatchGroup.Children.Add(mp_GPoint(3, 1));
                    oHatchGroup.Children.Add(mp_GPoint(1, 2));
                    oHatchGroup.Children.Add(mp_GPoint(2, 2));
                    oHatchGroup.Children.Add(mp_GPoint(3, 2));
                    oHatchGroup.Children.Add(mp_GPoint(0, 3));
                    oHatchGroup.Children.Add(mp_GPoint(1, 3));
                    oHatchGroup.Children.Add(mp_GPoint(3, 3));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_PERCENT75:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GPoint(2, 0));
                    oHatchGroup.Children.Add(mp_GPoint(0, 2));
                    InvertColors(ref clrForeColor, ref clrBackColor);
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_PERCENT80:
                    lWidth = 8;
                    lHeight = 7;
                    oHatchGroup.Children.Add(mp_GPoint(3, 0));
                    oHatchGroup.Children.Add(mp_GPoint(3, 4));
                    oHatchGroup.Children.Add(mp_GPoint(7, 2));
                    oHatchGroup.Children.Add(mp_GPoint(7, 6));
                    InvertColors(ref clrForeColor, ref clrBackColor);
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_PERCENT90:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(0, 5));
                    oHatchGroup.Children.Add(mp_GPoint(4, 1));
                    InvertColors(ref clrForeColor, ref clrBackColor);
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_LIGHTDOWNWARDDIAGONAL:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchGroup.Children.Add(mp_GPoint(1, 1));
                    oHatchGroup.Children.Add(mp_GPoint(2, 2));
                    oHatchGroup.Children.Add(mp_GPoint(3, 3));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_LIGHTUPWARDDIAGONAL:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GPoint(0, 3));
                    oHatchGroup.Children.Add(mp_GPoint(1, 2));
                    oHatchGroup.Children.Add(mp_GPoint(2, 1));
                    oHatchGroup.Children.Add(mp_GPoint(3, 0));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_DARKDOWNWARDDIAGONAL:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GPoint(0, 3));
                    oHatchGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchGroup.Children.Add(mp_GPoint(1, 1));
                    oHatchGroup.Children.Add(mp_GPoint(2, 2));
                    oHatchGroup.Children.Add(mp_GPoint(3, 3));
                    oHatchGroup.Children.Add(mp_GPoint(1, 0));
                    oHatchGroup.Children.Add(mp_GPoint(2, 1));
                    oHatchGroup.Children.Add(mp_GPoint(3, 2));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_DARKUPWARDDIAGONAL:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchGroup.Children.Add(mp_GPoint(0, 1));
                    oHatchGroup.Children.Add(mp_GPoint(1, 0));
                    oHatchGroup.Children.Add(mp_GPoint(1, 3));
                    oHatchGroup.Children.Add(mp_GPoint(2, 2));
                    oHatchGroup.Children.Add(mp_GPoint(3, 1));
                    oHatchGroup.Children.Add(mp_GPoint(2, 3));
                    oHatchGroup.Children.Add(mp_GPoint(3, 2));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_WIDEDOWNWARDDIAGONAL:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GLine(3, 0, 5, 0));
                    oHatchGroup.Children.Add(mp_GLine(4, 1, 6, 1));
                    oHatchGroup.Children.Add(mp_GLine(5, 2, 7, 2));
                    oHatchGroup.Children.Add(mp_GPoint(0, 3));
                    oHatchGroup.Children.Add(mp_GLine(6, 3, 7, 3));
                    oHatchGroup.Children.Add(mp_GLine(0, 4, 1, 4));
                    oHatchGroup.Children.Add(mp_GPoint(7, 4));
                    oHatchGroup.Children.Add(mp_GLine(0, 5, 2, 5));
                    oHatchGroup.Children.Add(mp_GLine(1, 6, 3, 6));
                    oHatchGroup.Children.Add(mp_GLine(2, 7, 4, 7));
                    oHatchCtrlGroup.Children.Add(mp_GPoint(7, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_WIDEUPWARDDIAGONAL:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GLine(4, 0, 6, 0));
                    oHatchGroup.Children.Add(mp_GLine(3, 1, 5, 1));
                    oHatchGroup.Children.Add(mp_GLine(2, 2, 4, 2));
                    oHatchGroup.Children.Add(mp_GLine(1, 3, 3, 3));
                    oHatchGroup.Children.Add(mp_GLine(0, 4, 2, 4));
                    oHatchGroup.Children.Add(mp_GLine(0, 5, 1, 5));
                    oHatchGroup.Children.Add(mp_GPoint(7, 5));
                    oHatchGroup.Children.Add(mp_GPoint(0, 6));
                    oHatchGroup.Children.Add(mp_GLine(6, 6, 7, 6));
                    oHatchGroup.Children.Add(mp_GLine(5, 7, 7, 7));
                    oHatchCtrlGroup.Children.Add(mp_GLine(0, 0, 7, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_LIGHTVERTICAL:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GLine(0, 0, 0, 3));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_LIGHTHORIZONTAL:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GLine(0, 0, 3, 0));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_NARROWVERTICAL:
                    lWidth = 2;
                    lHeight = 2;
                    oHatchGroup.Children.Add(mp_GLine(1, 0, 1, 1));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_NARROWHORIZONTAL:
                    lWidth = 2;
                    lHeight = 2;
                    oHatchGroup.Children.Add(mp_GLine(0, 1, 1, 1));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_DARKVERTICAL:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GLine(0, 0, 0, 3));
                    oHatchGroup.Children.Add(mp_GLine(1, 0, 1, 3));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_DARKHORIZONTAL:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GLine(0, 0, 3, 0));
                    oHatchGroup.Children.Add(mp_GLine(0, 1, 3, 1));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_DASHEDDOWNWARDDIAGONAL:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(0, 2));
                    oHatchGroup.Children.Add(mp_GPoint(1, 3));
                    oHatchGroup.Children.Add(mp_GPoint(2, 4));
                    oHatchGroup.Children.Add(mp_GPoint(3, 5));
                    oHatchGroup.Children.Add(mp_GPoint(4, 2));
                    oHatchGroup.Children.Add(mp_GPoint(5, 3));
                    oHatchGroup.Children.Add(mp_GPoint(6, 4));
                    oHatchGroup.Children.Add(mp_GPoint(7, 5));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_DASHEDUPWARDDIAGONAL:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(0, 7));
                    oHatchGroup.Children.Add(mp_GPoint(1, 6));
                    oHatchGroup.Children.Add(mp_GPoint(2, 5));
                    oHatchGroup.Children.Add(mp_GPoint(3, 4));
                    oHatchGroup.Children.Add(mp_GPoint(4, 7));
                    oHatchGroup.Children.Add(mp_GPoint(5, 6));
                    oHatchGroup.Children.Add(mp_GPoint(6, 5));
                    oHatchGroup.Children.Add(mp_GPoint(7, 4));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_DASHEDHORIZONTAL:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GLine(4, 0, 7, 0));
                    oHatchGroup.Children.Add(mp_GLine(0, 4, 3, 4));
                    oHatchCtrlGroup.Children.Add(mp_GPoint(7, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_DASHEDVERTICAL:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GLine(0, 0, 0, 1));
                    oHatchGroup.Children.Add(mp_GLine(0, 6, 0, 7));
                    oHatchGroup.Children.Add(mp_GLine(4, 2, 4, 5));
                    oHatchCtrlGroup.Children.Add(mp_GPoint(7, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_SMALLCONFETTI:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchGroup.Children.Add(mp_GPoint(4, 1));
                    oHatchGroup.Children.Add(mp_GPoint(1, 2));
                    oHatchGroup.Children.Add(mp_GPoint(6, 3));
                    oHatchGroup.Children.Add(mp_GPoint(3, 4));
                    oHatchGroup.Children.Add(mp_GPoint(7, 5));
                    oHatchGroup.Children.Add(mp_GPoint(2, 6));
                    oHatchGroup.Children.Add(mp_GPoint(5, 7));
                    oHatchCtrlGroup.Children.Add(mp_GPoint(7, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_LARGECONFETTI:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GLine(0, 1, 0, 2));
                    oHatchGroup.Children.Add(mp_GLine(0, 6, 0, 7));
                    oHatchGroup.Children.Add(mp_GLine(1, 6, 1, 7));
                    oHatchGroup.Children.Add(mp_GLine(2, 2, 2, 3));
                    oHatchGroup.Children.Add(mp_GLine(3, 2, 3, 3));
                    oHatchGroup.Children.Add(mp_GLine(3, 5, 3, 6));
                    oHatchGroup.Children.Add(mp_GLine(4, 0, 4, 1));
                    oHatchGroup.Children.Add(mp_GLine(4, 5, 4, 6));
                    oHatchGroup.Children.Add(mp_GLine(5, 0, 5, 1));
                    oHatchGroup.Children.Add(mp_GLine(6, 4, 6, 5));
                    oHatchGroup.Children.Add(mp_GLine(7, 1, 7, 2));
                    oHatchGroup.Children.Add(mp_GLine(7, 4, 7, 5));
                    oHatchCtrlGroup.Children.Add(mp_GPoint(7, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_ZIGZAG:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchGroup.Children.Add(mp_GPoint(1, 1));
                    oHatchGroup.Children.Add(mp_GPoint(2, 2));
                    oHatchGroup.Children.Add(mp_GLine(3, 3, 4, 3));
                    oHatchGroup.Children.Add(mp_GPoint(5, 2));
                    oHatchGroup.Children.Add(mp_GPoint(6, 1));
                    oHatchGroup.Children.Add(mp_GPoint(7, 0));

                    oHatchGroup.Children.Add(mp_GPoint(0, 4));
                    oHatchGroup.Children.Add(mp_GPoint(1, 5));
                    oHatchGroup.Children.Add(mp_GPoint(2, 6));
                    oHatchGroup.Children.Add(mp_GLine(3, 7, 4, 7));
                    oHatchGroup.Children.Add(mp_GPoint(5, 6));
                    oHatchGroup.Children.Add(mp_GPoint(6, 5));
                    oHatchGroup.Children.Add(mp_GPoint(7, 4));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_WAVE:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(2, 0));
                    oHatchGroup.Children.Add(mp_GPoint(5, 0));
                    oHatchGroup.Children.Add(mp_GPoint(7, 0));
                    oHatchGroup.Children.Add(mp_GLine(0, 2, 1, 2));

                    oHatchGroup.Children.Add(mp_GLine(3, 4, 4, 4));
                    oHatchGroup.Children.Add(mp_GPoint(2, 4));
                    oHatchGroup.Children.Add(mp_GPoint(5, 4));
                    oHatchGroup.Children.Add(mp_GPoint(7, 4));

                    oHatchGroup.Children.Add(mp_GLine(0, 6, 1, 6));
                    oHatchGroup.Children.Add(mp_GLine(3, 8, 4, 8));
                    oHatchCtrlGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchCtrlGroup.Children.Add(mp_GPoint(7, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_DIAGONALBRICK:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(0, 7));
                    oHatchGroup.Children.Add(mp_GPoint(1, 6));
                    oHatchGroup.Children.Add(mp_GPoint(2, 5));
                    oHatchGroup.Children.Add(mp_GPoint(3, 4));
                    oHatchGroup.Children.Add(mp_GPoint(4, 3));
                    oHatchGroup.Children.Add(mp_GPoint(5, 2));
                    oHatchGroup.Children.Add(mp_GPoint(6, 1));
                    oHatchGroup.Children.Add(mp_GPoint(7, 0));
                    oHatchGroup.Children.Add(mp_GPoint(4, 4));
                    oHatchGroup.Children.Add(mp_GPoint(5, 5));
                    oHatchGroup.Children.Add(mp_GPoint(6, 6));
                    oHatchGroup.Children.Add(mp_GPoint(7, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_HORIZONTALBRICK:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(4, 0));
                    oHatchGroup.Children.Add(mp_GPoint(4, 1));
                    oHatchGroup.Children.Add(mp_GLine(0, 1, 0, 5));
                    oHatchGroup.Children.Add(mp_GPoint(4, 6));
                    oHatchGroup.Children.Add(mp_GPoint(4, 7));

                    oHatchGroup.Children.Add(mp_GLine(1, 2, 7, 2));
                    oHatchGroup.Children.Add(mp_GLine(1, 6, 7, 6));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_WEAVE:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchGroup.Children.Add(mp_GPoint(1, 1));
                    oHatchGroup.Children.Add(mp_GPoint(2, 2));
                    oHatchGroup.Children.Add(mp_GPoint(0, 4));
                    oHatchGroup.Children.Add(mp_GPoint(1, 3));
                    oHatchGroup.Children.Add(mp_GPoint(3, 1));
                    oHatchGroup.Children.Add(mp_GPoint(4, 0));
                    oHatchGroup.Children.Add(mp_GPoint(5, 1));
                    oHatchGroup.Children.Add(mp_GPoint(6, 2));
                    oHatchGroup.Children.Add(mp_GPoint(7, 3));
                    oHatchGroup.Children.Add(mp_GPoint(5, 3));
                    oHatchGroup.Children.Add(mp_GPoint(4, 4));
                    oHatchGroup.Children.Add(mp_GPoint(3, 5));
                    oHatchGroup.Children.Add(mp_GPoint(2, 6));
                    oHatchGroup.Children.Add(mp_GPoint(1, 7));
                    oHatchGroup.Children.Add(mp_GPoint(3, 7));
                    oHatchGroup.Children.Add(mp_GPoint(5, 5));
                    oHatchGroup.Children.Add(mp_GPoint(6, 6));
                    oHatchGroup.Children.Add(mp_GPoint(7, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_PLAID:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GLine(0, 0, 3, 0));
                    oHatchGroup.Children.Add(mp_GLine(0, 1, 3, 1));

                    oHatchGroup.Children.Add(mp_GPoint(0, 2));
                    oHatchGroup.Children.Add(mp_GPoint(2, 2));
                    oHatchGroup.Children.Add(mp_GPoint(4, 2));
                    oHatchGroup.Children.Add(mp_GPoint(6, 2));

                    oHatchGroup.Children.Add(mp_GPoint(1, 3));
                    oHatchGroup.Children.Add(mp_GPoint(3, 3));
                    oHatchGroup.Children.Add(mp_GPoint(5, 3));
                    oHatchGroup.Children.Add(mp_GPoint(7, 3));


                    oHatchGroup.Children.Add(mp_GPoint(0, 4));
                    oHatchGroup.Children.Add(mp_GPoint(2, 4));
                    oHatchGroup.Children.Add(mp_GPoint(4, 4));
                    oHatchGroup.Children.Add(mp_GPoint(6, 4));

                    oHatchGroup.Children.Add(mp_GPoint(1, 5));
                    oHatchGroup.Children.Add(mp_GPoint(3, 5));
                    oHatchGroup.Children.Add(mp_GPoint(5, 5));
                    oHatchGroup.Children.Add(mp_GPoint(7, 5));

                    oHatchGroup.Children.Add(mp_GLine(0, 6, 3, 6));
                    oHatchGroup.Children.Add(mp_GLine(0, 7, 3, 7));
                    oHatchCtrlGroup.Children.Add(mp_GPoint(7, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_DIVOT:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(0, 1));
                    oHatchGroup.Children.Add(mp_GPoint(0, 3));
                    oHatchGroup.Children.Add(mp_GPoint(3, 5));
                    oHatchGroup.Children.Add(mp_GPoint(3, 7));
                    oHatchGroup.Children.Add(mp_GPoint(4, 6));
                    oHatchGroup.Children.Add(mp_GPoint(7, 2));
                    oHatchCtrlGroup.Children.Add(mp_GPoint(7, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_DOTTEDGRID:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(1, 6));
                    oHatchGroup.Children.Add(mp_GPoint(3, 6));
                    oHatchGroup.Children.Add(mp_GPoint(5, 6));
                    oHatchGroup.Children.Add(mp_GPoint(7, 6));
                    oHatchGroup.Children.Add(mp_GPoint(7, 4));
                    oHatchGroup.Children.Add(mp_GPoint(7, 2));
                    oHatchGroup.Children.Add(mp_GPoint(7, 0));
                    oHatchCtrlGroup.Children.Add(mp_GPoint(7, 7));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_DOTTEDDIAMOND:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(0, 0));
                    oHatchGroup.Children.Add(mp_GPoint(2, 2));
                    oHatchGroup.Children.Add(mp_GPoint(4, 4));
                    oHatchGroup.Children.Add(mp_GPoint(6, 6));
                    oHatchGroup.Children.Add(mp_GPoint(2, 6));
                    oHatchGroup.Children.Add(mp_GPoint(6, 2));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_SHINGLE:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GPoint(0, 3));
                    oHatchGroup.Children.Add(mp_GPoint(1, 4));
                    oHatchGroup.Children.Add(mp_GPoint(2, 5));
                    oHatchGroup.Children.Add(mp_GPoint(3, 5));
                    oHatchGroup.Children.Add(mp_GPoint(4, 6));
                    oHatchGroup.Children.Add(mp_GPoint(5, 6));
                    oHatchGroup.Children.Add(mp_GPoint(6, 7));
                    oHatchGroup.Children.Add(mp_GPoint(4, 4));
                    oHatchGroup.Children.Add(mp_GPoint(5, 3));
                    oHatchGroup.Children.Add(mp_GPoint(6, 2));
                    oHatchGroup.Children.Add(mp_GPoint(7, 2));
                    oHatchGroup.Children.Add(mp_GPoint(7, 0));
                    oHatchGroup.Children.Add(mp_GPoint(7, 1));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_TRELLIS:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GLine(0, 0, 3, 0));
                    oHatchGroup.Children.Add(mp_GLine(1, 1, 2, 1));
                    oHatchGroup.Children.Add(mp_GLine(0, 2, 3, 2));
                    oHatchGroup.Children.Add(mp_GPoint(0, 3));
                    oHatchGroup.Children.Add(mp_GPoint(3, 3));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_SPHERE:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GLine(1, 0, 3, 0));
                    oHatchGroup.Children.Add(mp_GLine(1, 1, 3, 1));
                    oHatchGroup.Children.Add(mp_GPoint(0, 2));
                    oHatchGroup.Children.Add(mp_GPoint(4, 2));
                    oHatchGroup.Children.Add(mp_GLine(1, 3, 2, 3));
                    oHatchGroup.Children.Add(mp_GPoint(0, 6));
                    oHatchGroup.Children.Add(mp_GPoint(4, 6));
                    oHatchGroup.Children.Add(mp_GLine(1, 7, 3, 7));
                    oHatchGroup.Children.Add(mp_GLine(5, 7, 6, 7));
                    oHatchGroup.Children.Add(mp_GLine(5, 3, 7, 3));
                    oHatchGroup.Children.Add(mp_GLine(5, 4, 7, 4));
                    oHatchGroup.Children.Add(mp_GLine(5, 5, 7, 5));
                    oHatchCtrlGroup.Children.Add(mp_GPoint(7, 7));
                    InvertColors(ref clrForeColor, ref clrBackColor);
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_SMALLGRID:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GLine(0, 0, 3, 0));
                    oHatchGroup.Children.Add(mp_GLine(0, 0, 0, 3));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_SMALLCHECKERBOARD:
                    lWidth = 4;
                    lHeight = 4;
                    oHatchGroup.Children.Add(mp_GRect(0, 0, 2, 2));
                    oHatchGroup.Children.Add(mp_GRect(2, 2, 2, 2));
                    yType = HatchEnum.HatchType.HT_RECTANGLE; break;
                case HatchEnum.HatchStyle.HS_LARGECHECKERBOARD:
                    lWidth = 8;
                    lHeight = 8;
                    oHatchGroup.Children.Add(mp_GRect(0, 0, 4, 4));
                    oHatchGroup.Children.Add(mp_GRect(4, 4, 4, 4));
                    yType = HatchEnum.HatchType.HT_RECTANGLE; break;
                case HatchEnum.HatchStyle.HS_OUTLINEDDIAMOND:
                    lWidth = 8;
                    lHeight = 8;

                    oHatchGroup.Children.Add(mp_GPoint(0, 4));
                    oHatchGroup.Children.Add(mp_GPoint(1, 3));
                    oHatchGroup.Children.Add(mp_GPoint(2, 2));
                    oHatchGroup.Children.Add(mp_GPoint(3, 1));
                    oHatchGroup.Children.Add(mp_GPoint(4, 0));

                    oHatchGroup.Children.Add(mp_GPoint(5, 1));
                    oHatchGroup.Children.Add(mp_GPoint(6, 2));
                    oHatchGroup.Children.Add(mp_GPoint(7, 3));

                    oHatchGroup.Children.Add(mp_GPoint(7, 5));
                    oHatchGroup.Children.Add(mp_GPoint(6, 6));
                    oHatchGroup.Children.Add(mp_GPoint(5, 7));

                    oHatchGroup.Children.Add(mp_GPoint(3, 7));
                    oHatchGroup.Children.Add(mp_GPoint(2, 6));
                    oHatchGroup.Children.Add(mp_GPoint(1, 5));
                    yType = HatchEnum.HatchType.HT_LINE; break;
                case HatchEnum.HatchStyle.HS_SOLIDDIAMOND:
                    lWidth = 7;
                    lHeight = 7;
                    oHatchGroup.Children.Add(mp_GRect(1, 1, 5, 5));
                    oHatchGroup.Transform = new RotateTransform(45, 3, 3);
                    yType = HatchEnum.HatchType.HT_RECTANGLE; break;
            }

            GeometryDrawing oBackgroundSquare = new GeometryDrawing(new SolidColorBrush(clrBackColor), null, new RectangleGeometry(new Rect(0, 0, lWidth, lHeight)));
            SolidColorBrush oHatchBrush = new SolidColorBrush(clrForeColor);
            Pen oHatchPen = new Pen(oHatchBrush, 1);
            oHatchBrush.Freeze();
            oHatchPen.Freeze();
            SolidColorBrush oHatchCtrlBrush = new SolidColorBrush(Colors.Red);
            Pen oHatchCtrlPen = new Pen(oHatchCtrlBrush, 1);
            oHatchCtrlBrush.Freeze();
            oHatchCtrlPen.Freeze();
            GeometryDrawing oHatch = null;
            GeometryDrawing oHatchCtrl = null;
            switch (yType)
            {
                case HatchEnum.HatchType.HT_RECTANGLE:
                    oHatch = new GeometryDrawing(oHatchBrush, null, oHatchGroup);
                    if (oHatchCtrlGroup.Children.Count > 0)
                    {
                        oHatchCtrl = new GeometryDrawing(oHatchCtrlBrush, null, oHatchCtrlGroup);
                    }
                    break;
                case HatchEnum.HatchType.HT_LINE:
                    oHatch = new GeometryDrawing(null, oHatchPen, oHatchGroup);
                    if (oHatchCtrlGroup.Children.Count > 0)
                    {
                        oHatchCtrl = new GeometryDrawing(null, oHatchCtrlPen, oHatchCtrlGroup);
                    }
                    break;

            }
            DrawingGroup oDrawingGroup = new DrawingGroup();
            if (bAliased)
            {
                oDrawingGroup.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
            }
            if (oHatchCtrl != null)
            {
                oDrawingGroup.Children.Add(oHatchCtrl);
            }
            oDrawingGroup.Children.Add(oBackgroundSquare);
            oDrawingGroup.Children.Add(oHatch);
            oReturnBrush.Drawing = oDrawingGroup;
            oReturnBrush.Stretch = Stretch.None;
            oReturnBrush.ViewportUnits = BrushMappingMode.Absolute;
            oReturnBrush.Viewport = new Rect(0, 0, lWidth, lHeight);
            oReturnBrush.TileMode = TileMode.Tile;
            oReturnBrush.Freeze();
            return oReturnBrush;
        }

        private LineGeometry mp_GLine(int X1, int Y1, int X2, int Y2)
        {
            if (X1 != X2)
            {
                X2 = X2 + 1;
            }
            if (Y1 != Y2)
            {
                Y2 = Y2 + 1;
            }
            LineGeometry oReturn = new LineGeometry(new Point(X1, Y1), new Point(X2, Y2)); ;
            return oReturn;
        }

        private LineGeometry mp_GPoint(int X1, int Y1)
        {
            LineGeometry oReturn = new LineGeometry(new Point(X1, Y1), new Point(X1 + 1, Y1 + 1)); ;
            return oReturn;
        }

        private RectangleGeometry mp_GRect(int X, int Y, int Width, int Height)
        {
            RectangleGeometry oReturn = new RectangleGeometry(new Rect(X, Y, Width, Height)); ;
            return oReturn;
        }

        private void InvertColors(ref Color clrForeColor, ref Color clrBackColor)
        {
            Color clrBuff;
            clrBuff = clrBackColor;
            clrBackColor = clrForeColor;
            clrForeColor = clrBuff;
        }
    }
The HatchType and HatchStyle should look like this:

/// <summary>
        /// Defines hatch type
        /// </summary>
        public enum HatchType
        {
            HT_RECTANGLE ,
        HT_LINE ,
        };

         /// <summary>
        /// Defines hatch style
        /// </summary>
        public enum HatchStyle
        {
             HS_HORIZONTAL ,
        HS_VERTICAL ,
        HS_FORWARDDIAGONAL ,
        HS_BACKWARDDIAGONAL ,
        HS_LARGEGRID ,
        HS_DIAGONALCROSS ,
        HS_PERCENT05 ,
        HS_PERCENT10 ,
        HS_PERCENT20 ,
        HS_PERCENT25,
        HS_PERCENT30 ,
        HS_PERCENT40 ,
        HS_PERCENT50 ,
        HS_PERCENT60 ,
        HS_PERCENT70 ,
        HS_PERCENT75 ,
        HS_PERCENT80 ,
        HS_PERCENT90 ,
        HS_LIGHTDOWNWARDDIAGONAL ,
        HS_LIGHTUPWARDDIAGONAL ,
        HS_DARKDOWNWARDDIAGONAL ,
        HS_DARKUPWARDDIAGONAL ,
        HS_WIDEDOWNWARDDIAGONAL ,
        HS_WIDEUPWARDDIAGONAL ,
        HS_LIGHTVERTICAL ,
        HS_LIGHTHORIZONTAL ,
        HS_NARROWVERTICAL ,
        HS_NARROWHORIZONTAL ,
        HS_DARKVERTICAL ,
        HS_DARKHORIZONTAL ,
        HS_DASHEDDOWNWARDDIAGONAL ,
        HS_DASHEDUPWARDDIAGONAL ,
        HS_DASHEDHORIZONTAL ,
        HS_DASHEDVERTICAL ,
        HS_SMALLCONFETTI ,
        HS_LARGECONFETTI ,
        HS_ZIGZAG ,
        HS_WAVE ,
        HS_DIAGONALBRICK ,
        HS_HORIZONTALBRICK ,
        HS_WEAVE ,
        HS_PLAID ,
        HS_DIVOT ,
        HS_DOTTEDGRID ,
        HS_DOTTEDDIAMOND ,
        HS_SHINGLE ,
        HS_TRELLIS ,
        HS_SPHERE ,
        HS_SMALLGRID ,
        HS_SMALLCHECKERBOARD ,
        HS_LARGECHECKERBOARD ,
        HS_OUTLINEDDIAMOND ,
        HS_SOLIDDIAMOND ,
        };


The Window1 would produce the following output:

Every effort has been made to ensure pixel by pixel accuracy in the WPF hatches, but this was not possible in all cases. In those cases where we could not achieve this, the best possible approximation was used.

Viewbox and Viewport in Windows Presentation Foundation

This original post is here

Viewbox and Viewport are two properties of ImageBursh, DrawingBrush, TileBrush and VisualBrush elements in Windows Presentation Foundation.  These two attributes let you to crop images and shapes and put them into your applications based on scales.
Viewbox represents an area in original image or drawing which we want to show in our application.  It gets four values: first two values specify the position of top left corner of area, third value specifies the width of area and fourth value specifies the height of area.
Viewport represents the target area in our application where the area that is selected by Viewbox will be shown.  Using Viewport you can manage the view of this area.  Like Viewbox it gets four values with same purposes.
There are two other properties related to Viewbox and Viewport.  ViewboxUnits determines that whether Viewbox values are relative (RelativeToBoundingBox) or absolute (Absolute) and default value is RelativeToBoundingBox.  ViewportUnits is the equivalent of ViewboxUnits for Viewport.
On relative boxing, units values for Viewbox or Viewport are doubles values between 0 and 1.  For example 0,0 is the top most left corner of an area and 1,1 is the right bottom corner of it.  On the other side 0.5, 0.5 for width and height of a 300×400 image equals to 150, 200.  On absolute boxing unit, values are based on pixels and can get integer values between zero and Integer.MaxValue.
Let's take a look at an example.  Below image is a 1024×768 sample picture in Windows Vista:
Javat
I name this bird Javat and want to select an area in this original image where Javat is sat and show it in my window.  So regarding above theories my code is this:
<Window x:Class="ViewboxViewport.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Viewbox and Viewport" Height="420" Width="400"
    >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="400" />
        Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="400" />
        Grid.ColumnDefinitions>
        <Rectangle Grid.Row="0" Grid.Column="0">
            <Rectangle.Fill>
                <ImageBrush ImageSource="Image.jpg" Viewbox="0.4,0.19,0.4,0.8"/>
            Rectangle.Fill>
        Rectangle>
    Grid>
Window>

Note that I could use Stretch attribute to stretch my image somehow.  For example Uniform value makes Javat's beak better:
<Window x:Class="ViewboxViewport.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Viewbox and Viewport" Height="420" Width="400"
    >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="400" />
        Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="400" />
        Grid.ColumnDefinitions>
        <Rectangle Grid.Row="0" Grid.Column="0">
            <Rectangle.Fill>
                <ImageBrush ImageSource="Image.jpg" Viewbox="0.4,0.19,0.4,0.8" Stretch="Uniform"/>
            Rectangle.Fill>
        Rectangle>
    Grid>
Window>

Now I add a Viewport attribute to change the area where my image will be shown:
<Window x:Class="ViewboxViewport.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Viewbox and Viewport" Height="420" Width="400"
    >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="400" />
        Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="400" />
        Grid.ColumnDefinitions>
        <Rectangle Grid.Row="0" Grid.Column="0">
            <Rectangle.Fill>
                <ImageBrush ImageSource="Image.jpg" Viewbox="0.4,0.19,0.4,0.8" Viewport="0.05,0.05,0.85,0.85" />
            Rectangle.Fill>
        Rectangle>
    Grid>
Window>

And finally let's take a look at an absolute boxing unit example:
<Window x:Class="ViewboxViewport.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Viewbox and Viewport" Height="420" Width="400"
    >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="400" />
        Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="400" />
        Grid.ColumnDefinitions>
        <Rectangle Grid.Row="0" Grid.Column="0">
            <Rectangle.Fill>
                <ImageBrush ImageSource="Image.jpg"
                Viewbox="420,180,430,530" ViewboxUnits="Absolute"
                Viewport="20,20,350,350" ViewportUnits="Absolute" />
            Rectangle.Fill>
        Rectangle>
    Grid>
Window>

Thứ Hai, 27 tháng 2, 2012

Để giao tiếp - Ứng xử thành công

Trong cuộc sống, trong giao tiếp hàng ngày con người luôn phải ứng phó với biết bao tình huống, có lúc dễ dàng xử lý, có lúc thật phức tạp, khó xử. Xã hội càng văn minh thì nhu cầu trong giao tiếp của con người càng cao. Ứng xử một cách thông minh, khôn khéo, tế nhị, kịp thời, có hiệu quả, đạt tới mức độ nghệ thuật, ngày nay còn được coi như bí quyết thành công trong cuộc sống, trong công việc .
Để giao tiếp, ứng xử thành công hơn bạn nên chú ý những nguyên tắc sau:
I. Nguyên tắc ứng xử:
1. Hãy tiếp cận với con người ở góc độ không tốt, không xấu:
Không có ai là người hoàn toàn xấu cả. Khi đánh giá con người cụ thể chúng ta thường bị những cách nhìn tĩnh tại, xơ cứng, bị định kiến che lấp, ít khi tách ra được dù chỉ là tương đối đầy đủ những ưu điểm, nhược điểm của họ và lại càng không xác định được giới hạn, hoàn cảnh, diễn biến có thể có của những ưu điểm và nhược điểm đó. Con người cũng dễ bị chi phối bởi qui luật cảm xúc "yêu nên tốt, ghét nên xấu". Kết quả là dễ mắc sai lầm trong giao tiếp. Do vậy nguyên tắc đầu tiên trong nghệ thuật ứng xử là hãy tiếp cận với con người ở góc độ không tốt, không xấu.
Ở nguyên tắc này, mỗi người có thể tự tìm cho mình một cách ứng xử thành công, ta tạm chia thành các bước sau:
  • Bước 1: Hãy thừa nhận (chấp nhận).
  • Bước 2: Biết lắng nghe ý kiến, tìm ra chỗ mạnh, chỗ yếu của người. Thấy cái mạnh, lợi thế của ta (cái ta có mà người không có).
  • Bước 3: Tạo ra sự đồng cảm, gây niềm tin (hiểu biết lẫn nhau, gây sự tin tưởng).
  • Bước 4: Tìm điểm chung, mỗi bên đều thấy được cái lợi, cái vui và cách cộng tác, tương lai của sự cộng tác đó.
  • Bước 5: Tạo dư luận ủng hộ, xây dựng mối quan hệ thân tình.
2. Lường mọi điều, tính đến mọi khả năng với nhiều phương án:
Nếu trong mỗi con người đều có những giá trị dương (+) (những đức tín tốt, những mặt mạnh, những ưu điểm...) và có cả những giá trị âm (-) (những tính xấu, những mặt yếu, những khuyết điểm...) thì với người này có thể tới 90 (+), có 10 (-) hoặc ở người khác có thể đạt 99 (+) chỉ có 1 (-). Vấn đề là ở chỗ cần phải biết nhìn ra "dấu cộng" trong cả khối "dấu trừ" và phát hiện kịp thời "dấu trừ" trong "vô khối dấu cộng" để có thể dự đoán được tác động có hại của mặt trái "dấu trừ" mà khởi thủy của nó chỉ là một chấm nhỏ rất mờ nhạt trong cả khối "dấu cộng".
Điều quan trọng hơn trong phép ứng xử là tìm ra những chất xúc tác để kích thích phản ứng đổi dấu tích cực xảy ra theo hướng 1 (+) trở thành n (+) và n (-) giảm xuống còn 1 (-).
3. Nắm bắt nghệ thuật theo nhu cầu:
Điều khó nhất trong giao tiếp ứng xử là đối tượng thờ ơ, không có nhu cầu. Bạn sẽ ứng xử như thế nào khi đối tượng không muốn nói chuyện, không muốn nghe bạn can ngăn, không muốn hợp tác với bạn v.v...?
- Hãy gợi trí tò mò hoặc cho họ thấy cái lợi, cái vui mà bạn đang có, còn người ấy đang thiếu, đang cần.
- Chỉ cho họ bằng cách nào đó, thấy tia hy vọng vào kết quả, gây thiện cảm, tạo sự tin tưởng.
- Giao trách nhiệm một cách công khai, tạo ra tình huống chỉ có tiến chứ không có lùi, ràng buộc bằng những sợi dây vô hình về quan hệ nào đó đã được hình thành.
- Gây niềm say mê, tìm được ý nghĩa trong cuộc sống, công việc đang tiến hành.
- Củng cố niềm tin, thuyết phục về một kết cục tốt đẹp.
- Phải tạo ra sự an toàn, biết cách chống đỡ dư luận, tạo dư luận mới ủng hộ.
- Tính đến nhiều phương án, chọn phương án hợp với mình nhất, có thể tạo ra những vấp ngã nhỏ để luyện tập bản lĩnh, chủ động đề phòng tính tự kiêu.
Trong những trường hợp cụ thể đòi hỏi mọi người phải biết ứng biến và điều quan trọng hơn là hãy tìm cho được sơ đồ đi tới thành công riêng của mình.
II. Một số cách ứng xử:
1. Thủ thuật "ném đá thăm đường":
Có bạn trai tâm sự: Đến dự đám cưới một người bạn, tôi đã gặp một người con gái mà tôi thầm mơ ước, tôi rất muốn làm quen với người con gái ấy nhưng không biết phải bắt đầu như thế nào?
Có lẽ trước tiên nên bắt đầu vào đề từ cùng một hứng thú. Chẳng hạn có thể từ một bức tranh, một bản nhạc, một ca khúc mới được mọi người yêu thích, hay một bộ phim mới, một quyển sách mới, một mốt quần áo mới... những cái đối phương biết rõ và có phản ứng tích cực. Hoặc có thể từ một điểm chung nào đó về học tập, việc làm... Bắt đầu từ những câu chuyện không quan trọng lắm để làm mất đi sự căng thẳng và ngăn cách giữa hai người và qua đó bạn có thể hiểu thêm về người bạn mà ta muốn gần gũi (những sở thích, thói quen, cá tính...) có một sơ đồ giao tiếp sau đây:
a. Giai đoạn trước khi giao tiếp:
- Xác định mục đích, làm quen đối tượng và gây ấn tượng tốt
- Đánh giá đối tượng, hoàn cảnh:
  • Sở thích,thói quen, cá tính.
  • Thời gian, không gian cuộc gặp.
  • Có hay không có người giới thiệu.
- Lựa chọn phương án ứng xử:
  • Tìm cớ làm quen cho hợp lý, tế nhị.
  • Ngôn ngữ, cử chỉ dễ cảm mến.
  • Văn phong gần gũi, không quá trịnh trọng nhưng cũng không suồng sã.
  • Tính trước những tình huống bất ngờ có thể xảy ra để ứng xử nhanh.
b. Giai đoạn giao tiếp:
Nên:
  • Phá bầu không khí xa lạ bằng việc hỏi han hoặc những câu nói đùa.
  • Tìm chủ đề chung, ít chạm đến quan điểm cá nhân.
  • Nói ít về mình. Chú ý quan sát cử chỉ, điệu bộ, nét mặt của đối phương.
  • Giới hạn cuộc giao tiếp trong phạm vi vừa đủ, dừng đúng lúc.
  • Nêu ra những câu hỏi thăm dò, chủ động gợi chuyện.
  • Tự tin.
Không nên:
  • Vội vã đi vào vấn đề chính.
  • Đặt những câu hỏi liên quan nhiều đến cá nhân.
  • Nói nhiều về mình, dốc bầu tâm sự, nói thẳng về mình khi đối phương đặt những câu hỏi thăm dò.
  • Chuyện lan man, kéo dài cuộc giao tiếp.
  • Khích bác hoặc công kích nói xấu một ai đó.
  • Rụt rè, lảng tránh, ấp úng.
2. Tình huống cần đối đáp mềm mỏng, ý nghĩa sâu xa:
Dân gian có câu:
"Lời nói chẳng mất tiền mua
Lựa lời mà nói cho vừa lòng nhau".

Đối với nhiều ý kiến phê bình, phản đối của đối phương, không nên đáp lại bằng những lời nói hằn học, nặng nề mà nhiều khi nên dùng lời nói nhẹ nhàng nhưng chứa đựng những ý nghĩa sâu xa.
3. Tình huống phải chuyển bại thành thắng:
Trong cuộc sống đời thườnng nhiều khi ta bị đẩy vào tình huống bất lợi, có nguy cơ thất bại, lúc đó đòi hỏi phải bình tĩnh, suy nghĩ ngay đến những hậu quả xấu nhất có thể xảy ra (chuẩn bị tâm thế sẵn sàng chấp nhận). Tìm xem có cách gì để hạn chế mức thấp nhất những tác hại (ví dụ: điều gì đã đẩy ta vào tình thế bất lợi, có cách nào tạo được kế hoãn binh có vẻ ít liên quan, nhưng nếu được "địch thủ" sẵn sàng chấp nhận thì chính điều có vẻ không liên quan đó có thể thay đổi tình thế...).
4. Tình huống dùng hài hước:
"Khi bạn nổi cáu ta hãy đùa lại một câu" (Laphôngten).
Hài hước là một nhân tố cực kỳ quan trọng trong ngôn ngữ giao tiếp. Đó là "chiếc van an toàn" cho mọi cuộc xung đột, là chìa khóa để mở "cánh cửa lòng". Lời đối đáp khôn ngoan, thông minh, dùng ngôn ngữ hài hước để phê phán thường mang lại hiệu quả lớn hơn nhiều. Bởi thế khi kể một câu chuyện cười hoặc một lời đối đáp có nội dung, cách nói hài hước thường làm cho không khí vui nhộn, điều tiết được tình cảm, nhắc khéo người khác mà không làm họ bực mình. Tất nhiên cũng không nên lạm dụng nó.
Quy luật phổ biến của truyện hài hước là mở đầu dẫn dắt và hình thành làm cho người ta nghi vấn. Người kể nên có ngữ điệu bình thường, sau đó tăng thêm tình tiết nghi hoặc và giải quyết bất ngờ.
5. Tình huống phải đi thẳng vào vấn đề khi cần thiết:
Trong cuộc sống có trường hợp không thể quanh co, bóng gió, tế nhị mà phải bày tỏ quan điểm, thái độ của mình một cách thẳng thắn, kiên quyết. Lúc đó phải diễn đạt vào thẳng nội dung chính của vấn đề để biểu hiện ý chí và lòng tin ở bản thân. Đối với những vấn đề then chốt không nên tỏ ra quá cân nhắc, đắn đo làm cho người nghe cảm thấy thiếu tin tưởng, do dự. Tất nhiên để nói bằng cách này cần phải suy nghĩ, cân nhắc thật kỹ càng.
6. Tình huống nói ẩn ý bằng ngụ ngôn:
Trong giao tiếp khi cảm thấy khó thuyết phục người khác bằng lý lẽ trực tiếp hoặc cảm thấy dễ bị phản ứng, không tiện nói thẳng ra, thì người ta thường dùng phương pháp ẩn ý bằng ngụ ngôn. Tức là chọn những câu chuyện ngụ ngôn có nội dung ẩn ý bên trong phù hợp với mục đích khuyên răn, thuyết phục của mình để kể cho đối phương nghe.
Cái lợi của phương pháp này là người nghe phải suy nghĩ mới hiểu hết cái ẩn ý bên trong đó. Bản thân câu chuyện sẽ đưa ra những lời khuyên sâu sắc chứ không phải người kể chuyện, do đó không có lý do để nổi khùng, tự ái hoặc mặc cảm.
Tuy nhiên để dùng phương pháp này có hiệu quả, người dùng phương pháp này phải am hiểu câu chuyện phù hợp với trình độ người nghe, nếu người nghe không hiểu gì cả, sẽ không có tác dụng.
7. Tình huống phản bác khéo những yêu cầu vô lý ở người khác:
Cũng có lúc bạn gặp những người khăng khăng đưa ra cho bạn những đòi hỏi vô lý không thể thực hiện được. Trước những tình huống đó nhiều khi ta không thể bác bỏ thẳng thừng vì chạm lòng tự ái hoặc người không thỏa mãn với ta rồi tiếp tục quấy rối nữa và cách đó cũng chưa làm cho người tự nhận thấy được những đòi hỏi của họ là vô lý.
Vậy ta sẽ xử lý thế nào trong trường hợp đó? Tốt nhất là hãy thừa nhận đã, sau đó khéo léo chỉ ra sự vô lý hoặc điều không thể thực hiện được. Cũng có thể cảnh tỉnh người đó bằng việc chỉ ra những điều bất lợi, sự nguy hiểm nếu người đó cứ giữ nguyên ý kiến, nhắm mắt hành động. Chú ý ngôn ngữ không nên gay gắt nhưng tỏ ra cương quyết.
8. Tình huống thừa nhận trước để chuyển hướng sau:
Nếu khi bạn không đồng ý với ý kiến của đối phương mà người đó lại là cấp trên, người lớn tuổi, cha mẹ... thì bạn sẽ xử sự như thế nào? Việc thuyết phục để đối phương nghe theo mình, có nghĩa là chấp nhận ý kiến của mình cũng đòi hỏi phải có một nghệ thuật nhất định.
Bạn chớ phản đối và phê phán các ý kiến của đối phương. Bạn hãy tiếp thu ý kiến của họ, biểu thị thái độ đồng cảm ở mức độ nào đó để có thể làm giảm được sự cứng nhắc của đối phương, khiến họ bằng lòng nghe ý kiến của bạn. Song phải nắm vững nguyên tắc không được tỏ thái độ của mình ngang bằng với đối phương để tiếp sau đó dùng lời mà chuyển hướng, thay đổi cách nhìn nhận của đối phương, làm họ bằng lòng tiếp thu ý kiến của bạn.
9. Tình huống cần bạn đồng minh:
Khi tranh luận trước nhiều người cần thể hiện quan điểm, bạn nên chú ý đầy đủ đến thái độ của những người xung quanh, cần động viên được nhiều người nghe và ủng hộ quan điểm của mình. Nếu người nghe ủng hộ ta, đồng tình với quan điểm của ta đang trình bày, sẽ tạo thành một sức mạnh to lớn, một sức ép tinh thần làm đối phương không phản kích lại được.
Chẳng hạn khi đang xếp hàng có người chen ngang, bạn lợi dụng thái độ của số đông mọi người xung quanh để gạt người đó ra khỏi hàng là hợp lý nhất.
10. Tình huống không nhượng bộ khi mình có lý trong tranh luận:
Trong quan hệ giữa người với người, tranh luận là một điều hết sức bình thường và không thể tránh được. Không có tranh luận, điều phải trái không được phân định. Không thể coi tranh luận là một thói xấu mà hạn chế nó.
Song tranh luận có thể dẫn đến sự không thoải mái hoặc đôi khi xung đột. Tranh luận có phương pháp sẽ đem lại kết quả tốt là điều chúng ta cần chú ý học hỏi.
Một là, khi tranh luận nên có thái độ thật công bằng, đừng làm tổn thương đến lòng tự ái của người kia. Sự phê phán, bình phẩm người khác không thể quá một giới hạn nhất định, nếu không có thể làm tăng thêm mâu thuẫn vốn có.
Hai là, giọng nói phải mền mỏng, thật lòng. Trong tranh luận phải tỏ ra tôn trọng nhau, làm sao cho người cùng tranh luận tin rằng tranh luận thật là có ích. Trong tranh luận nhiều khi người thắng không hẳn đã là nhiều lý lẽ biết hùng biện, mà có thể là người có thái độ đúng mực và chân thực nhất.
Ba là, tranh luận phải có mục đích rõ ràng. Tranh luận nên xoay quanh những điều cần giải quyết.
11. Tình huống cần thuyết phục bằng hành động:
"Mọi lý thuyết đều màu xám
Còn cây đời mãi mãi xanh tươi"
(Gơt)
Trong giao tiếp, khi cảm thấy khó thuyết phục người khác nghe ý kiến của mình bằng lời nói, bạn có thể dùng hành động để thuyết phục. Thuyết phục bằng hành động thường hiệu quả lớn nhất. Thông qua việc làm, hành động cụ thể, ta có thể làm cho đối phương thay đổi cách nghĩ, tình cảm, thái độ, chấp nhận ý kiến của ta.
Bạn là một cán bộ phong trào thanh niên. Bạn muốn tổ chức các hoạt động văn hóa thể dục thể thao thanh niên nhưng các cấp lãnh đạo ở địa phương chưa tin tưởng vào khả năng của bạn, chưa tạo điều kiện mọi mặt để bạn làm việc. Bạn đừng nản chí và cũng đừng dùng lời nói để thuyết phục. Hãy cố gắng tạo ra một vài việc làm cụ thể có hiệu quả. Từ sự thành công đã đạt được, tận dụng thời điểm gây hưng phấn cao rồi đưa ra những kiến giải hợp lý với các cấp lãnh đạo.
Như vậy, mục đích và kết quả hoạt động đạt được và mối quan hệ ảnh hưởng của bạn phát triển tốt hơn.
Khi vận dụng phương pháp này bạn cần lưu ý:
  • Mục đích hoạt động phải rõ ràng, không vụ lợi.
  • Có kế hoạch hành động chi tiết, tính đến các điều kiện cần và đủ đảm bảo cho sự thành công, bước đầu tiên tránh thất bại.
  • Tạo dư luận ủng hộ để gây sức ép hoặc quy tụ sức mạnh.
  • Làm thử để chứng minh, rút kinh nghiệm.
  • Tạo quan hệ gần gũi tin cẩn.
Nếu ở vào tình huống các bậc "hụ huynh" của người yêu bạn còn chưa tin tưởng và chấp nhận cho bạn yêu con của họ, thì bạn cũng có thể dùng phương pháp này để đạt mục đích.
III. Những điều nên tránh trong giao tiếp ứng xử:
1. Nói nửa chừng rồi dừng lại hoặc cướp lời người đang nói, làm nhiễu thứ tự hoặc luồng suy nghĩ của người đó.
2. Không nói rõ và giải thích đầy đủ làm người nghe cảm thấy đột ngột, khó hiểu đề tài nói chuyện của bạn. Không nên đưa những trọng tâm, những khái quát làm người tiếp chuyện khó theo dõi mạch chuyện.
3. Nói sai đề tài, không quan tâm đến điều mình nói.
4. Nói thao thao bất tuyệt, không ngừng nêu các câu hỏi làm người tiếp chuyện có cảm giác mình yêu cầu hơi nhiều quá.
5. Không trả lời thẳng vào câu hỏi mà người khác nêu ra, quanh co, dài dòng, gây nên cảm giác không trung thực cho người hỏi.
6. Tự cho rằng mọi điều mình đều biết cả.
7. Làm ra vẻ hiểu biết sâu rộng.
8. Phát triển câu chuyện không tập trung vào chủ đề chính làm cho người tiếp chuyện cảm thấy nhàm chán.9. Ngắt bỏ hứng thú nói chuyện của người khác để ép người đó phải chuyển sang nói về đề tài mà bạn thích.
10. Thì thầm với một vài người trong đám đông.
11. Dùng ngôn ngữ quá bóng bảy.
12. Chêm những câu tiếng nước ngoài trong câu nói của mình một cách tùy tiện.
13. Đột ngột cao giọng.
14. Dùng những lời quá suồng sã với mức độ quan hệ.
15. Dùng những từ đệm không cần thiết.
16. Nói với giọng khích bác, chạm vào lòng tự ái của người khác. 

Thứ Tư, 22 tháng 2, 2012

Bộ sách HEAD FIRST

Mấy quyển sách này đc viết theo kiểu học ngoại ngữ hồi cấp 1 ấy , đa phần là minh họa bằng tranh ảnh nên rất dễ hiểu .Có đủ cả phần lý thuyết và bài tập (lab) .Ngôn ngữ rất funny nên đọc ko bị cảm giác chán. Recommend đến bạn nào muốn đọc trước một môn nào đó trước khi học trên lớp sẽ khá hay.


 
http://www.mediafire.com/headfirst
Nguồn: http://forums.updatesofts.com

Thứ Năm, 17 tháng 2, 2011

Quickly Resize Images in Windows 7 and Vista on the Fly

Resizing image has always been a problem in Windows OS, it lacks the support to resize image quickly. Fortunately the popular tool ImageResizer in Windows XP now has support in Windows 7 and Vista, it has native 64 bit support for 64 bit 7 and Vista.
Like it sounds, this tool lets you quickly resize a selection of multiple images at once. It’s was typically used in the past to compress photos to more manageable sizes for sharing via email (although that’s not necessary anymore since Windows Live Mail does this for you). Now, it’s more likely to be used to compress photos down for use on your web page or blog.
Simply download the installer and you will have the option to resize any image when you right click the image. You can also select multiple images and resize.resize_image
You should see the new "Resize Pictures" option after you done installing the application
resize_pictures
It has 4 different presents build in and of course you can customize the size of the image by going to the advanced option
advanced_view_resize
Dwonload ImageResizer for Windows 7/Vista 32 bit
Dwonload ImageResizer for Windows 7/Vista 64 bit
Source Via Channel 10

Install Oracle 11g on Windows 7

I finally got around to installing Oracle 11g on Windows 7 Professional Edition, 64-bit. The only catch was that the Oracle 11g (11.1.0.7) installer validates only against Windows 5.0, 5.1, 5.2 or 6.0. Windows 7 is actually Windows Code Base 6.1, as seen in this screen shot after successful installation:
Windows7VersionScreen Oracle 11g on Windows 7
With that knowledge, first you should download the software from Oracle’s web site. You should unzip the contents into another directory. I used a C:\Stage directory. Inside that you’ll find the database directory, and it should look like this:
Oracle11gFolder Oracle 11g on Windows 7
  1. Click on the setup icon to launch the installer. You should then see the following screen shot. Enter a password for the SYS and SYSTEM users (if you’re coming to Oracle from a MySQL background they’re like the root user in MySQL). When you’ve entered a matching password in both fields, the Install button becomes active. Click the Install button to proceed.
Oracle11gInstall01 Oracle 11g on Windows 7
  1. In this screen, you’re prompted for your Email and MetaLink Password. If this is a test database (a throw away instance without any real data), you can uncheck the box for automatic security updates. You should enter a password even for test databases. Click the Next button to proceed.
Oracle11gInstall02 Oracle 11g on Windows 7
  1. You’ll only see this failure if you’re installing Oracle 11g (11.1.0.7) on a Windows 7 operating system, which is actually version 6.1 according to their code control numbering. You simply check the Checking operating system requirements … and Checking service pack requirements … to override the prerequisite checks. Another error that you may encounter is related to networking. It is a warning and occurs when you’re operating system uses DHCP to get its IP address. You really should configure the operating system with a static IP address. You can set a static IP address in Windows 7 with these instructions.
Oracle11gInstall03 Oracle 11g on Windows 7
The checked boxes now say User Verified, which means we’re all really powerful, aren’t we? icon smile Oracle 11g on Windows 7 Click the Next button to proceed.
Oracle11gInstall04 Oracle 11g on Windows 7
  1. At this point, you’ll get a Windows System Alert asking you to unblock the installer’s javaw.exe program. You must grant the access or forget about installing Oracle 11g. Click the Allow access button to proceed.
Oracle11gInstall05 Oracle 11g on Windows 7
  1. The dialog tells you what will be installed. Click the Install button to proceed.
Oracle11gInstall06 Oracle 11g on Windows 7
  1. This is the installation progress dialog. It’s running while the Oracle Installer lays down the operating system files for the database management system. It takes about 8 to 9 minutes, so stretch your legs if you want to take a break. When it’s 100% complete, click the Next button to continue.
Oracle11gInstall07 Oracle 11g on Windows 7
  1. After the installation, you’ll see the first of the Configuration Assistants, which is the Oracle Net Configuration Assistant (unfortunately, I didn’t capture that screen shot but I provided a temporary substitute until I re-install it). Provided everything happens successfully, it’ll move to the next step without your intervention.
Oracle11gInstall08a Oracle 11g on Windows 7
You should receive another Windows Security Alert (aren’t we glad that I already disabled UAC). You need to click the Allow access button to let java.exe proceed with the installation.
Oracle11gInstall08b Oracle 11g on Windows 7
  1. This one is exactly where it belongs. It means you have approximately a 5 to 6 minute break while a sample database instance is cloned for you. The cloning process copies a sample compressed database from the installation staging area to your local operating system.
Oracle11gInstall09a Oracle 11g on Windows 7
It will prompt you if you want to open any of the other scheme. You can skip this and do it later, or click Password Management to open those scheme and set passwords for them.
Oracle11gInstall09b Oracle 11g on Windows 7
  1. The last Configuration Assistant is the Oracle Configuration Manager Configuration. It’s a short process, it configures the cloned sample database against your installation names and passwords. When it completes it enables the Next button. Click the Next button to complete the installation.
Oracle11gInstall10 Oracle 11g on Windows 7
  1. You’ve finally reached almost the end of the installation. Click the Exit button on this dialog to proceed to the “are you sure” dialog box.
Oracle11gInstall11 Oracle 11g on Windows 7
Confirm you meant it, by clicking the Yes button on the final dialog message below.
Oracle11gInstall12 Oracle 11g on Windows 7
You’ve successfully installed Oracle 11g on Windows 7.

Thứ Ba, 7 tháng 12, 2010

Configuring Remote Administration and Feature Delegation in IIS 7

Introduction

IIS 7 (IIS 7.0 on Windows Server® 2008 or IIS 7.5 on Windows Server 2008 R2) provides administrators and developers with a new configuration system that is accessible, extensible and distributable. The new XML-based format allows for easy configuration of the modules and features available in IIS 7. It also enables granular control of the locations at which settings for individual features can be configured (for example, at the server level in the applicationHost.config file, or at a site or an application level in a Web.config file).
The new IIS 7 administration user interface (UI), IIS Manager, fully supports this new configuration system and adds additional features that provide a powerful and granular system for configuring the Web server. Two of these additional features are remote administration of servers, sites and applications, and support for user-based authentication and authorization.
This article explains how to enable remote connections, configure users and permissions, and delegate features to a site or application level. There are many scenarios where an IIS 7 server administrator might want to delegate administrative control of a certain feature or features to someone, or where the administrator might want to prevent others from viewing existing configuration. Take the following scenario, for example.
Edward is a server administrator on a computer that hosts several sites. The computer is part of a domain, and some of the site owners belong to the same domain. However, some of the site owners are outside of the domain, and Edward must create IIS Manager user accounts for them by creating a user name and password for each owner. After he has created the necessary IIS Manager user accounts, Edward sets up IIS Manager permissions for each site to specify which of the users are allowed to connect to a particular site. To do this, Edward opens the IIS Manager Permissions feature in each site and adds Windows users and IIS Manager users. This action does two things. First, it configures IIS 7 to allow a user to connect to the site when the user provides valid credentials. Second, it permits users who successfully connect to configure any delegated features in that site.
Edward also wants to delegate configuration of some features that he trusts to be configured by a site owner in his or her own site. This eliminates the need for a site owner to request that Edward configure features that vary by site, such as default documents. He decides to delegate configuration for the following features in all sites on his server: Default Documents, Directory Browsing, and Error Pages. In addition, Edward decides to delegate configuration of an additional feature, HTTP Redirect, to the site Contoso, because he knows that the site often needs to be redirected and trusts the site owners to configure those settings. He sets all other features to be Read Only so that site owners can see the settings but not configure them in their sites.
Julian and Catherine are site owners for the site Contoso on Edward's computer. Julian has a Windows user account, and Catherine has an IIS Manager user account for which Edward has provided her credentials. They can both open IIS Manager on their own computers, and connect to Contoso because Edward has permitted their accounts to configure the Contoso site. They each see all of the features that have been delegated to the site level. They can configure Default Documents, Directory Browsing, Error Pages, and HTTP Redirect because Edward delegated configuration of those settings to their site.

Prerequisites

The following items are required to be installed to complete the procedures in this article:
  • IIS 7.0 on Windows Server® 2008 or IIS 7.5 on Windows Server 2008 R2
  • IIS Manager

Configuring Remote Connections in IIS Manager

In IIS 7, IIS Manager makes it easy to perform remote administration. In addition to managing IIS 7 on the local computer, IIS Manager can manage remote servers, sites, and applications. The server administrator uses remote administration features to add IIS Manager user accounts and to allow those users to connect to any sites or applications for which they have permission.
Configuring remote administration involves enabling remote connections in IIS Manager and configuring the type of credentials required to connect to the server. Optionally, you can change default connection and logging settings, and add connection restrictions based on IP addresses or domain names.
Install the Management Service
The default IIS 7 installation options do not include the Management Service (also referred to as the Web Management Service (WMSVC)), which is required for remote administration. If you have not installed the Management Service, follow the steps in this procedure to install it.
To install the Management Service:
  1. Click Start, type Server Manager in the Search box, and press ENTER to open Server Manager.
  2. In the tree, under Roles, select Web Server (IIS).
  3. Click Add role services, and then select Management Service as shown in the image below.
  4. Click Next and follow the instructions to complete the installation.


Enable Remote Connections and Configure Identity Credentials

Enable remote connections so that Windows users and IIS Manager users (configured later in this article) can connect to this computer by using IIS Manager on their computers. By default, the Management Service allows connections only from users that have Windows credentials, but you can configure it to also allow connections from users with IIS Manager credentials. For the purposes of this article, configure the Management Service to allow both credential types as shown in the image below.
Note: The next section in this article explains IIS Manager credentials.    
To enable remote connections and allow connections from Windows users and IIS Manager users:
  1. In IIS Manager, in the Connections pane, click the server node in the tree.
  2. Double-click Management Service to open the Management Service feature page.

     4 .Select the Enable remote connections check-box.
     5. Under Identity Credentials, select Windows credentials or IIS Manager credentials.
     6. In the Actions pane, click Apply to save the changes, and then click Start to start the Management Service.

Additional Information
You do not have to enable remote connections to start the Management Service. If remote connections are disabled and the Management Service is started, you can connect to the Management Service from the local computer but not from a remote computer. If you cannot connect from a remote computer, make sure that remote connections are enabled.
You should check your firewall settings to ensure that connections are allowed to the Management Service. When the Management Service is installed, the setup process adds a firewall rule that allows traffic to the Management Service on port 8172 (the default port) which is on by default. If you ever change the port that the Management Service uses, you must add a new firewall rule to allow traffic to the Management Service on that port.

Configure Connection and Logging Settings for the Management Service

In addition to the Management Service settings that were configured in the above section, you can configure connection settings and specify where to log requests. The following table describes each field and its default setting. If you change any settings, make sure to click Apply in the Actions pane and then restart the Management Service.
PropertyDescriptionDefault Setting
IP AddressSpecifies the IP address to which the service is bound.All Unassigned
PortSpecifies the port number that the service uses for requests.8172
SSL CertificateSpecifies the SSL certificate used by the service. All requests to the service use HTTPS over the port specified in the Port field. This list contains the SSL certificates that are available to the server.
If you want to add additional SSL certificates, use the Server Certificates feature at the server level.
Self-signed certificate that is installed during setup
Log requests toSpecifies the path to the log files for the Management Service.%SystemDrive%\Inetpub\logs\WMSVC

Configure IP and Domain Restrictions for the Management Service

By default, the Management Service accepts all requests made to its configured IP address and port, and users are allowed to connect when they are added to IIS Manager (as explained in the next section). However, you can configure the service to deny access for unspecified requests and instead add specific allow rules so that only requests made from a specific IP address or domain are accepted. For more information about allowing or denying requests from IP addresses or domains, see the procedures under Configuring Remote Management on Microsoft TechNet.

Configuring Users and Permissions for IIS Manager

When you configured the Management Service in the above section, you selected the Windows credentials or IIS Manager credentials option. This option enables users with either Windows user accounts or IIS Manager user accounts to connect to a site or application on the remote computer by using IIS Manager. Both types of users must provide valid credentials (a user name and password pair) when they connect remotely. A Windows user must provide valid Windows credentials for a user account on the remote computer or for a user account in the domain if the computer is a member of a domain. An IIS Manager user must provide valid IIS Manager credentials, which are configured in IIS Manager by a server administrator on the remote computer. In both cases, a user is able to use IIS Manager to connect to sites or applications for which the server administrator has given the user permission.

Add an IIS Manager User

The following procedures explain how to open the IIS Manager Users feature and add a user. When the IIS Manager Users feature page opens, the list displays each IIS Manager user name and whether the account is enabled or disabled. Only enabled accounts are allowed to connect to sites or applications for which they have been granted permission.
To add an IIS Manager user:
  1. In IIS Manager, in the Connections pane, click the server node in the tree.
  2. On the server home page, double-click IIS Manager Users.

    3. On the IIS Manager Users page, in the Actions pane, click Add User.
    4. In the User name box, type a user name.
    5. In the Password box, type a password and then retype the password in the Confirm password box.
    6. Click OK.


Configure IIS Manager Permissions for a Site or an Application

In order for a user to remotely connect to a site or application on the server, they must be granted permission to a site or application by the server administrator. After they are granted permission, they can use IIS Manager to connect to the site or application using either their Windows credentials (if they are a Windows user) or their IIS Manager user credentials (if they are an IIS Manager user).
To permit an IIS Manager user to connect to a site or an application:
1. In IIS Manager, in the Connections pane, select the site or application for which you want to configure permissions.
2. On the home page for the site or application, double-click IIS Manager Permissions.


3. On the IIS Manager Permissions page, in the Actions pane, click Allow User.
4. On the Allow User dialog box, select IIS Manager and click Select.

5. On the Users dialog box, select one or more IIS Manager users from the list and then click OK.

 6. Click OK to dismiss the Allow User dialog box.
To permit a Windows user to connect to a site or an application:
1. On the IIS Manager Permissions page, in the Actions pane, click Allow User.
2. On the Allow User dialog box, select Windows and then click Select.
3. On the Select User or Group dialog box, type a user name or search for a user account, and then click OK.

4. Click OK to dismiss the Allow User dialog box.

Configure Access Control Lists (ACLs) for Content Directories

In order for IIS Manager to work correctly when a user connects to a site or an application, the ACLs must be configured properly for the physical directory of the site or application. For Windows users, you must configure directory and file ACLs for each Windows user or group that needs to access the directories and files. For IIS Manager users, you must configure directory and file ACLs for the WMSVC user (NT Service\WMSVC by default).
For the physical directory of a site or an application, configure read, write, and execute permissions for the WMSVC user (if you are permitting IIS Manager users to connect) and for any Windows users who need to connect to that site or application.
If the physical directory of the site or application is on a different machine (i.e. on a UNC share) then you will need to configure WMSVC to run as a user which has read, write, and execute permissions for the content on that UNC share (the default user, NT Service\WMSVC, cannot access items on a different machine).

Connect to a Site or an Application in IIS Manager

After you have configured the Management Service and configured users and permissions, a user can connect to his or her site or application.
To connect to a site or an application
1. In IIS Manager, click File and then click Connect to a Site (or Connect to an Application).
2. On the Connect to Site or Connect to Application wizard, type the server name and the site name to which you want to connect. If you are connecting to an application, type the application name also. Then click Next.

3. On the Provide Credentials page of the Connect to Site or Connect to Application wizard, select whether you want to use current credentials or to specify the credentials to connect to the site. When you specify credentials, the default is Windows credentials unless you select the Use IIS Manager credentials check-box. After you specify the credentials, click Next to connect to the server.

4. If the connection succeeds, IIS Manager will display a final page on the Connect to Site or Connect to Application wizard to name the connection. As shown in the image below, the TestAdmin user has created a site connection to the Contoso.com site.
 
Additional Information
In the next section, we look at Feature Delegation. However, before we do that, and to better explain Feature Delegation and what it does, we look at one feature of the site we have just connected to. The site should be highlighted and the site home page should be displayed. On the home page, double-click Error Pages.
As shown in the image below, on the right-hand side of the page there is an alert saying "This feature has been locked and is read only." This alert is displayed when a feature has been locked; the next section explains Feature Delegation and locking in more detail.


Delegating Features in IIS Manager

The Feature Delegation feature page lets a server administrator configure the delegation state of features that are configurable in Web.config files at the site and application levels in IIS Manager. He or she might want to delegate configuration of certain features, such as Default Documents and Directory Browsing, to an individual site owner so that the owner can configure those delegated features in his or her site.
Or a server administrator might delegate a feature as read only so that a site owner can view the configuration of a feature but cannot change the settings. The server administrator can even prevent features from appearing in IIS Manager at the site and application levels.
There are several different delegation states, each of which determines whether a feature is delegated to lower levels in the configuration system and whether the feature displays in IIS Manager when users are connected at lower levels. The following table describes each delegation state.
Delegation TypeDescription
Not DelegatedThe configuration is locked and any configuration of the feature in a Web.config file will cause a runtime error.
The feature is not visible or configurable in IIS Manager when a user is connected at levels below where this state is set. For example, if a feature is configured to be Not Delegated at a site level, users connected to applications in that site will not see the feature and cannot configure it in IIS Manager.
Read OnlyThe configuration is locked and any configuration of the feature in a Web.config file will cause a runtime error.
The feature is visible in IIS Manager when a user is connected at lower levels, but the configuration is locked so that changes cannot be made.
Read/WriteThe feature can be configured in Web.config.
The feature appears in IIS Manager and can be configured when a user is connected at lower levels (site or application level).
Configuration Read OnlyThe meaning is the same as Read Only; however, there are settings or data for the feature that are stored and managed outside of IIS, such as in a database.
Configuration Read/WriteThe meaning is the same as Read/Write; however, there are settings or data for the feature that are stored and managed outside of IIS, such as in a database.
Note: Server administrators can modify the configuration for all features, so if they are connected to the server, they will see all features at all levels even if a feature has been configured to not display at lower levels.

Configure Default Delegation States for Features in IIS Manager

When you first open the Feature Delegation feature page, you can configure the default delegation state of features in IIS Manager. These delegation states are the default settings used by IIS Manager for all sites and applications on the server.
To configure default delegation states for features in IIS Manager
1. In IIS Manager, in the Connections pane, click the server node in the tree.
2. On the server home page, double-click Feature Delegation.

3. Select Delegation from the Group by list to organize the list of features by their current delegation states.

4. Select Error Pages in the Feature Delegation list, and then review the available delegation states in the Actions pane. The Error Pages feature is selected and the available options under Set Feature Delegation are Read/Write, Remove Delegation and Reset to Inherited.

5. Select Error Pages. In the Actions pane, click Read/Write to unlock the configuration section that is related to the Error Pages feature. This makes the feature configurable in Web.config files and in IIS Manager at the site and application levels.

Additional Information

The following excerpt from the IIS configuration files shows that the values can be overridden at every location (also referred to as "path"):

<location path="" overrideMode="Allow">
    <system.webServer>
        <httpErrors>
            ...
            ...
        </httpErrors>
    </system.webServer>
</location>
  

 
To further demonstrate what this action does, look at Error Pages feature at a site level. In the Connections pane, connect to a site and then double-click Error Pages on the site home page. As shown in the image below, the Error Pages feature can now be configured by the user connected to the site level.

Configure Custom Delegation States for Features in a Site or an Application

The steps above configured the default delegation state for the Error Pages feature in all sites on the server. There may also be a time when you do not want the delegation state that you configure to be applicable to all sites. In this case, you can configure a custom delegation state for a specific site. You can also copy custom delegation states of all features from one site to another site.
To configure custom delegation states for features in a specific site
1. In IIS Manager, double-click Feature Delegation.
2. On the Feature Delegation page, in the Actions pane, click Custom Web Site Delegation.
3. From the Sites list, select the site for which you want to configure custom delegation settings.

4. Select a feature, and then click a delegation state in the Actions pane.
To copy custom delegation states from one site to another site
  1. On the Custom Web Site Delegation page, in the Sites list, select the site from which you want to copy delegation to another site.
  2. Click Copy Delegation.
  3. On the Copy Delegation dialog box, select the site or sites to which you want to copy the delegation states and then click OK.

Reset Feature Delegation States

There may be a time in which it is necessary to undo the changes that have been made to the delegation states of features. Perhaps the changes were made to a particular site or application by accident, or some 'experimentation' with the feature has gone wrong. In this case, reset the delegation states of all features back to their default states. Or, reset only one feature back to its default state.

To reset feature delegation states
  1. Open the Feature Delegation feature page.
  2. To reset all feature delegation states, in the Actions pane click Reset All Delegation.
To reset the delegation state of a specific feature, select the feature in the list, and in the Actions pane click Reset to Inherited.