Arquivos para a Categoria ‘WPF’

h1

InertiaScrollViewer – ScrollViewer com inercia! (UPDATED)

Março 13, 2009

Aqui fica o novo codigo do InertiaScrollViewer, tem algumas melhorias principalmente em termos de performance.

    public class InertiaScrollViewer : ScrollViewer
    {
        DispatcherTimer _yourTimer;

        public InertiaScrollViewer()
        {

            _yourTimer = new DispatcherTimer(DispatcherPriority.Normal);
            _yourTimer.Interval = TimeSpan.FromMilliseconds(10);
            _yourTimer.Tick += new EventHandler(yourTimer_Tick);

            this.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
            this.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
        }

        #region Overrides

        protected override void OnPreviewMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {

            Point mouseposition = e.GetPosition(this);
            InertiaScrollViewer.SetIsDragging(this, true);
            InertiaScrollViewer.SetTargetX(this, mouseposition.X * -1);
            InertiaScrollViewer.SetTargetY(this, mouseposition.Y * -1);
            double offsetx = this.HorizontalOffset + mouseposition.X;
            double offsety = this.VerticalOffset + mouseposition.Y;
            InertiaScrollViewer.SetOffset(this, new Point(offsetx, offsety));
            _yourTimer.Start();
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
        {
            Point mouseposition = e.GetPosition(this);
            InertiaScrollViewer.SetTargetX(this, (mouseposition.X) * -1);
            InertiaScrollViewer.SetTargetY(this, (mouseposition.Y) * -1);
            InertiaScrollViewer.SetIsDragging(this, false);
            base.OnMouseUp(e);
        }

        protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
        {
            InertiaScrollViewer.SetIsDragging(this, false);
            base.OnMouseLeave(e);
        }

        protected override void OnPreviewMouseMove(System.Windows.Input.MouseEventArgs e)
        {
            if (!CanDrag && (InertiaScrollViewer.GetIsDragging(this) == true))
            {
                Point mouseposition = e.GetPosition(this);
                InertiaScrollViewer.SetTargetX(this, (mouseposition.X) * -1);
                InertiaScrollViewer.SetTargetY(this, (mouseposition.Y) * -1);
                _yourTimer.Start();
            }
            base.OnMouseMove(e);
        }

        #endregion

        private void yourTimer_Tick(object sender, EventArgs e)
        {

            if (!CanDrag)
            {
                this.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                this.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                if (InertiaScrollViewer.GetIsDragging(this))
                {
                    double oldX = this.HorizontalOffset;
                    double oldY = this.VerticalOffset;

                    this.ScrollToVerticalOffset(this.VerticalOffset + (((InertiaScrollViewer.GetTargetY(this) + InertiaScrollViewer.GetOffset(this).Y) - this.VerticalOffset) * .3));
                    this.ScrollToHorizontalOffset(this.HorizontalOffset + ((InertiaScrollViewer.GetTargetX(this) + InertiaScrollViewer.GetOffset(this).X) - this.HorizontalOffset) * .3);

                    InertiaScrollViewer.SetVelocityX(this, (this.HorizontalOffset + ((InertiaScrollViewer.GetTargetX(this) + InertiaScrollViewer.GetOffset(this).X) - this.HorizontalOffset) * .3) - oldX);
                    InertiaScrollViewer.SetVelocityY(this, (this.VerticalOffset + (((InertiaScrollViewer.GetTargetY(this) + InertiaScrollViewer.GetOffset(this).Y) - this.VerticalOffset) * .3)) - oldY);
                }
                else
                {
                        this.ScrollToHorizontalOffset(this.HorizontalOffset + InertiaScrollViewer.GetVelocityX(this));
                        this.ScrollToVerticalOffset(this.VerticalOffset + InertiaScrollViewer.GetVelocityY(this));

                        InertiaScrollViewer.SetVelocityX(this, InertiaScrollViewer.GetVelocityX(this) * VelocityConst);
                        InertiaScrollViewer.SetVelocityY(this, InertiaScrollViewer.GetVelocityY(this) * VelocityConst);
                }

            }
            if ((Math.Abs(Math.Round(InertiaScrollViewer.GetVelocityX(this), 1)) == 0.0
                && Math.Abs(Math.Round(InertiaScrollViewer.GetVelocityY(this), 1)) == 0.0)
                || (this.ScrollableHeight==this.VerticalOffset && this.ScrollableWidth==this.HorizontalOffset)
                || (0 == this.VerticalOffset && 0 == this.HorizontalOffset))
            {
                _yourTimer.Stop();
                this.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
                this.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
            }
        }

        #region Properties
        #region TargetX
        public static double GetTargetX(DependencyObject obj)
        {
            return (double)obj.GetValue(TargetXProperty);
        }

        public static void SetTargetX(DependencyObject obj, double value)
        {
            obj.SetValue(TargetXProperty, value);
        }

        // Using a DependencyProperty as the backing store for TargetX.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TargetXProperty =
            DependencyProperty.Register("TargetX", typeof(double), typeof(InertiaScrollViewer), new UIPropertyMetadata(0.0));

        #endregion

        #region TargetY
        public static double GetTargetY(DependencyObject obj)
        {
            return (double)obj.GetValue(TargetYProperty);
        }

        public static void SetTargetY(DependencyObject obj, double value)
        {
            obj.SetValue(TargetYProperty, value);
        }

        // Using a DependencyProperty as the backing store for TargetY.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TargetYProperty =
            DependencyProperty.Register("TargetY", typeof(double), typeof(InertiaScrollViewer), new UIPropertyMetadata(0.0));

        #endregion

        #region Offset

        public static Point GetOffset(DependencyObject obj)
        {
            return (Point)obj.GetValue(OffsetProperty);
        }

        public static void SetOffset(DependencyObject obj, Point value)
        {
            obj.SetValue(OffsetProperty, value);
        }

        // Using a DependencyProperty as the backing store for Offset.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OffsetProperty =
            DependencyProperty.Register("Offset", typeof(Point), typeof(InertiaScrollViewer), new UIPropertyMetadata(new Point(0, 0)));
        #endregion

        #region Velocity

        public static double GetVelocityX(DependencyObject obj)
        {
            return (double)obj.GetValue(VelocityXProperty);
        }

        public static void SetVelocityX(DependencyObject obj, double value)
        {
            obj.SetValue(VelocityXProperty, value);
        }

        // Using a DependencyProperty as the backing store for VelocityX.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VelocityXProperty =
            DependencyProperty.RegisterAttached("VelocityX", typeof(double), typeof(InertiaScrollViewer), new UIPropertyMetadata(0.0));

        public static double GetVelocityY(DependencyObject obj)
        {
            return (double)obj.GetValue(VelocityYProperty);
        }

        public static void SetVelocityY(DependencyObject obj, double value)
        {
            obj.SetValue(VelocityYProperty, value);
        }

        // Using a DependencyProperty as the backing store for VelocityY.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VelocityYProperty =
            DependencyProperty.Register("VelocityY", typeof(double), typeof(InertiaScrollViewer), new UIPropertyMetadata(0.0));

        public double VelocityConst
        {
            get { return (double)GetValue(VelocityConstProperty); }
            set { SetValue(VelocityConstProperty, value); }
        }

        // Using a DependencyProperty as the backing store for VelocityConst.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VelocityConstProperty =
            DependencyProperty.Register("VelocityConst", typeof(double), typeof(InertiaScrollViewer), new UIPropertyMetadata(0.97));

        #endregion

        #region IsDragging

        public static bool GetIsDragging(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsDraggingProperty);
        }

        public static void SetIsDragging(DependencyObject obj, bool value)
        {
            obj.SetValue(IsDraggingProperty, value);
        }

        // Using a DependencyProperty as the backing store for IsDragging.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsDraggingProperty =
            DependencyProperty.Register("IsDragging", typeof(bool), typeof(InertiaScrollViewer), new UIPropertyMetadata(false));

        #endregion

        public bool CanDrag
        {
            get { return (bool)GetValue(CanDragProperty); }
            set { SetValue(CanDragProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CanDrag.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CanDragProperty =
            DependencyProperty.Register("CanDrag", typeof(bool), typeof(InertiaScrollViewer), new UIPropertyMetadata(false));

        #endregion
    }

Até breve,
Miguel Duarte

h1

InertiaListBox – ListBox com inercia!!!

Março 13, 2009

Com o InertiaScrollViewer que podem encontrar mais abaixo tinhamos um problema ao usa-lo numa ListBox devido aos eventos do Selector, para isso resolvi criar esta classe que nos permite ter o mesmo comportamento mas agora com uma ListBox.

Portanto, cá esta o código do .cs

  [TemplatePart(Name = "PART_Scroll", Type = typeof(InertiaScrollViewer))]
    public class InertiaListBox : ListBox
    {

        #region Declara-se que
        private SelectionChangedEventArgs _selectionChangedEventArgs;
        private Point _offset;
        private Point _mouseDown;
        List<object> _oldSelecteds;
        #endregion

        static InertiaListBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(InertiaListBox), new FrameworkPropertyMetadata(typeof(InertiaListBox)));
        }

        public InertiaListBox()
        {
            _offset = new Point(0.0, 0.0);
            _selectionChangedEventArgs = null;
            _oldSelecteds = new List<object>();
            this.PreviewMouseDown += new MouseButtonEventHandler(InertiaListBox_PreviewMouseDown);
            Style _styleItems = new Style(typeof(ListBoxItem));
            TemplateBindingExtension b=new TemplateBindingExtension(InertiaListBox.IsSelectedProperty);
            _styleItems.Setters.Add(new Setter(Selector.IsSelectedProperty,b));
        }

        #region Eventos

        void InertiaListBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            _offset = new Point((this.Template.FindName("PART_Scroll", this) as InertiaScrollViewer).HorizontalOffset,
               (this.Template.FindName("PART_Scroll", this) as InertiaScrollViewer).VerticalOffset);
            _mouseDown = new Point(Mouse.GetPosition(this).X + _offset.X,
                Mouse.GetPosition(this).Y + _offset.Y);
        }

        #endregion

        #region Auxiliar
        private void SetIsSelected(SelectionChangedEventArgs e)
        {

                foreach (var item in _oldSelecteds)
                {
                    InertiaListBox.SetIsSelected(this.ItemContainerGenerator.ContainerFromItem(item), false);
                }
                _oldSelecteds = new List<object>();
                if (e != null)
                {
                    foreach (var item in e.AddedItems)
                    {
                        _oldSelecteds.Add(item);
                        InertiaListBox.SetIsSelected(this.ItemContainerGenerator.ContainerFromItem(item), true);
                    }
                }
        }
        #endregion

        #region Overrides

        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
            Point _mouseUp = new Point(Mouse.GetPosition(this).X + _offset.X,
               Mouse.GetPosition(this).Y + _offset.Y);
            if (_selectionChangedEventArgs != null)
            {
                switch (Orientation)
                {
                    case Orientation.Horizontal:
                        if (Math.Abs(_mouseUp.X - _mouseDown.X) < 2)
                        {
                            SetIsSelected(_selectionChangedEventArgs);
                            base.OnSelectionChanged(_selectionChangedEventArgs);
                            _selectionChangedEventArgs = null;
                        }
                        break;
                    case Orientation.Vertical:
                        if (Math.Abs(_mouseUp.Y - _mouseDown.Y) < 2)
                        {
                            SetIsSelected(_selectionChangedEventArgs);
                            base.OnSelectionChanged(_selectionChangedEventArgs);
                            _selectionChangedEventArgs = null;
                        }
                        break;
                    default:
                        break;
                }
            }

            base.OnMouseUp(e);
        }

        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            _selectionChangedEventArgs = e;
        }

        #endregion

        #region Properties
        public static new bool GetIsSelected(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsSelectedProperty);
        }

        public static new void SetIsSelected(DependencyObject obj, bool value)
        {
            obj.SetValue(IsSelectedProperty, value);
        }

        // Using a DependencyProperty as the backing store for IsSelected.  This enables animation, styling, binding, etc...
        public static new readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.RegisterAttached("IsSelected", typeof(bool), typeof(InertiaListBox), new UIPropertyMetadata(false));

        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Orientation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(InertiaListBox), new UIPropertyMetadata(Orientation.Vertical));
        #endregion

    }

O template onde usamos o InertiaSrollviewer

<Style TargetType="{x:Type local:InertiaListBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:InertiaListBox}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <local:InertiaScrollViewer Focusable="False" x:Name="PART_Scroll">
                           <StackPanel Orientation="{TemplateBinding Orientation}" IsItemsHost="True" />
                        </local:InertiaScrollViewer>
                    </Border>
                </ControlTemplate
            </Setter.Value>
        </Setter>
    </Style>

A partir de agora e sempre que quisermos usa-la, basta definir o ItemTemplate ou o Style para ListBoxItem, por exemplo

<local:InertiaListBox  Orientation="Vertical">
            <local:InertiaListBox.Resources>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <TextBlock x:Name="mov" Text="{Binding .}"/>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="local:InertiaListBox.IsSelected" Value="True">
                                        <Setter Property="Background" TargetName="mov" Value="Blue"/>
                                        <Setter Property="Foreground" TargetName="mov" Value="White"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                    </Style.Triggers>
                </Style>
            </local:InertiaListBox.Resources>
        </local:InertiaListBox>

Para já é tudo, confiram os updates ao InertiaScrollViewer :)
Até breve,
Miguel Duarte

h1

InertiaScrollViewer – ScrollViewer com inercia!

Fevereiro 26, 2009

Com este ScrollViewer temos a capacidade de fazer scroll apenas com o arrastar do rato
Updated

public class InertiaScrollViewer : ScrollViewer
    {
        DispatcherTimer _yourTimer;

        public InertiaScrollViewer()
        {
            _yourTimer = new DispatcherTimer();
            _yourTimer.Interval = new TimeSpan(0, 0, 0, 0, 2);
            _yourTimer.Tick += new EventHandler(yourTimer_Tick);
            _yourTimer.Start();
            this.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
            this.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
        }

        #region Overrides

        protected override void OnPreviewMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            InertiaScrollViewer.SetIsDragging(this, true);
            InertiaScrollViewer.SetTargetX(this, e.GetPosition(this).X);
            InertiaScrollViewer.SetTargetY(this, e.GetPosition(this).Y);
            double offsetx = this.HorizontalOffset - e.GetPosition(this).X;
            double offsety = this.VerticalOffset - e.GetPosition(this).Y;
            InertiaScrollViewer.SetOffset(this, new Point(offsetx, offsety));
        }

        protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);

            InertiaScrollViewer.SetTargetX(this, (e.GetPosition(this).X));
            InertiaScrollViewer.SetTargetY(this, (e.GetPosition(this).Y));
            InertiaScrollViewer.SetIsDragging(this, false);
        }

        protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            InertiaScrollViewer.SetIsDragging(this, false);
        }

        protected override void OnPreviewMouseMove(System.Windows.Input.MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if ((InertiaScrollViewer.GetIsDragging(this) == true))
            {
                InertiaScrollViewer.SetTargetX(this, (e.GetPosition(this).X));
                InertiaScrollViewer.SetTargetY(this, (e.GetPosition(this).Y));
            }
        }

        #endregion

        private void yourTimer_Tick(object sender, EventArgs e)
        {
            if (InertiaScrollViewer.GetIsDragging(this))
            {
                double oldX = this.HorizontalOffset;
                double oldY = this.VerticalOffset;

                this.ScrollToVerticalOffset(this.VerticalOffset + (((InertiaScrollViewer.GetTargetY(this) + InertiaScrollViewer.GetOffset(this).Y) - this.VerticalOffset) * .3));
                this.ScrollToHorizontalOffset(this.HorizontalOffset + ((InertiaScrollViewer.GetTargetX(this) + InertiaScrollViewer.GetOffset(this).X) - this.HorizontalOffset) * .3);

                InertiaScrollViewer.SetVelocityX(this, (this.HorizontalOffset + ((InertiaScrollViewer.GetTargetX(this) + InertiaScrollViewer.GetOffset(this).X) - this.HorizontalOffset) * .3) - oldX);
                InertiaScrollViewer.SetVelocityY(this, (this.VerticalOffset + (((InertiaScrollViewer.GetTargetY(this) + InertiaScrollViewer.GetOffset(this).Y) - this.VerticalOffset) * .3)) - oldY);
            }
            else
            {
                if (Math.Abs(Math.Round(InertiaScrollViewer.GetVelocityX(this), 3)) != 0.000)
                {
                    this.ScrollToHorizontalOffset(this.HorizontalOffset + InertiaScrollViewer.GetVelocityX(this));
                    this.ScrollToVerticalOffset(this.VerticalOffset + InertiaScrollViewer.GetVelocityY(this));

                    InertiaScrollViewer.SetVelocityX(this, InertiaScrollViewer.GetVelocityX(this) * VelocityConst);
                    InertiaScrollViewer.SetVelocityY(this, InertiaScrollViewer.GetVelocityY(this) * VelocityConst);
                }
            }
        }

        #region Properties
        #region TargetX
        public static double GetTargetX(DependencyObject obj)
        {
            return (double)obj.GetValue(TargetXProperty);
        }

        public static void SetTargetX(DependencyObject obj, double value)
        {
            obj.SetValue(TargetXProperty, value);
        }

        // Using a DependencyProperty as the backing store for TargetX.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TargetXProperty =
            DependencyProperty.Register("TargetX", typeof(double), typeof(InertiaScrollViewer), new UIPropertyMetadata(0.0));

        #endregion

        #region TargetY
        public static double GetTargetY(DependencyObject obj)
        {
            return (double)obj.GetValue(TargetYProperty);
        }

        public static void SetTargetY(DependencyObject obj, double value)
        {
            obj.SetValue(TargetYProperty, value);
        }

        // Using a DependencyProperty as the backing store for TargetY.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TargetYProperty =
            DependencyProperty.Register("TargetY", typeof(double), typeof(InertiaScrollViewer), new UIPropertyMetadata(0.0));

        #endregion

        #region Offset

        public static Point GetOffset(DependencyObject obj)
        {
            return (Point)obj.GetValue(OffsetProperty);
        }

        public static void SetOffset(DependencyObject obj, Point value)
        {
            obj.SetValue(OffsetProperty, value);
        }

        // Using a DependencyProperty as the backing store for Offset.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OffsetProperty =
            DependencyProperty.Register("Offset", typeof(Point), typeof(InertiaScrollViewer), new UIPropertyMetadata(new Point(0, 0)));
        #endregion

        #region Velocity

        public static double GetVelocityX(DependencyObject obj)
        {
            return (double)obj.GetValue(VelocityXProperty);
        }

        public static void SetVelocityX(DependencyObject obj, double value)
        {
            obj.SetValue(VelocityXProperty, value);
        }

        // Using a DependencyProperty as the backing store for VelocityX.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VelocityXProperty =
            DependencyProperty.RegisterAttached("VelocityX", typeof(double), typeof(InertiaScrollViewer), new UIPropertyMetadata(0.0));

        public static double GetVelocityY(DependencyObject obj)
        {
            return (double)obj.GetValue(VelocityYProperty);
        }

        public static void SetVelocityY(DependencyObject obj, double value)
        {
            obj.SetValue(VelocityYProperty, value);
        }

        // Using a DependencyProperty as the backing store for VelocityY.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VelocityYProperty =
            DependencyProperty.Register("VelocityY", typeof(double), typeof(InertiaScrollViewer), new UIPropertyMetadata(0.0));

        public double VelocityConst
        {
            get { return (double)GetValue(VelocityConstProperty); }
            set { SetValue(VelocityConstProperty, value); }
        }

        // Using a DependencyProperty as the backing store for VelocityConst.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VelocityConstProperty =
            DependencyProperty.Register("VelocityConst", typeof(double), typeof(InertiaScrollViewer), new UIPropertyMetadata(0.96));

        #endregion

        #region IsDragging

        public static bool GetIsDragging(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsDraggingProperty);
        }

        public static void SetIsDragging(DependencyObject obj, bool value)
        {
            obj.SetValue(IsDraggingProperty, value);
        }

        // Using a DependencyProperty as the backing store for IsDragging.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsDraggingProperty =
            DependencyProperty.Register("IsDragging", typeof(bool), typeof(InertiaScrollViewer), new UIPropertyMetadata(false));

        #endregion
        #endregion
    }

Para usar basta definir no Template que queremos usar o nosso InertiaScrollViewer

<ItemsControl>
                <ItemsControl.Template>
                    <ControlTemplate>
                        <local:InertiaScrollViewer>
                            <StackPanel IsItemsHost="True"/>
                        </local:InertiaScrollViewer>
                    </ControlTemplate>
                </ItemsControl.Template>
            </ItemsControl>

Em breve vou colocar um Canvas exactamente com o mesmo funcionamento, mas para os UiElements que estão dentro dele…
Até breve,
Miguel Duarte

h1

Tranformar Array Bytes em BitmapImage

Março 26, 2007

Deixo-vos aqui uma pequena função que nos permite transformar um Byte[] em BitmapImage, bastante útil quando guardamos imagens em base de dados ou transferindo por webServices!!

/// <summary>

/// Obter uma imagem atravez de um array de bytes

/// </summary>

/// <param name=”arraybytes”>array de bytes</param>

/// <returns>devolve um BitmapImage que podemos adicionar á source </returns>

/// <example>Obj.source=getImage(byte[])</example>

BitmapImage getImage(byte[] arraybytes)

{

    // Criar a Imagem

    // NOTE: Isto n é um Bitmap normal (GDI+)

    BitmapImage bitmap = new BitmapImage();

    if (arraybytes != null)

    {

        MemoryStream strm = new MemoryStream(arraybytes);

        bitmap.BeginInit();

        bitmap.StreamSource = strm;

        bitmap.EndInit();

    }

    //COMMENT: Retorna a Imagem

   return bitmap;

}

h1

Blend Release Candidate

Março 16, 2007

Já está diponível  para download a nova RC do Microsoft Blend que pode ser descarregada em:

http://www.microsoft.com/products/expression/en/expression-blend/try.mspx

Aproveito também para deixar uns vídeos a todos aqueles que tal como eu se pretendem iniciar em WPF com umas brincadeiras engraçadas.

Real-world WPF: Introduction to Blend (Part1)

Real-world WPF: Introduction to Blend (Part2)

Real-world WPF: Introduction to Blend (Part3)

Real-world WPF: Introduction to Blend (Part4)

h1

TECH DAYS – 2007 – A minha Agenda!

Março 16, 2007
h1

O que fazer com um projecto Antigo??

Março 12, 2007

Aqui vamos meu primeiro Post, e vou começar por uma questão que fiz a mim mesmo quando comecei a desenvolver em wpf num projecto “a sério”.

O que vou fazer com os módulos que já tenho, recomeçar tudo de novo??

A resposta a esta pergunta é simples… não… posso integrar XAML em Windows Forms e Windows Forms (Aqui quando me refiro a Windows Forms estou-me a referir controls/Usercontrols) em XAML.
Para isso basta usar o namespace System.Windows.Forms.Integration

Para integrar XAML em Windows Forms fazemos:

//Declarar/Construir o Host para colacar XAML dentro de uma Windows Form Framework 2.0
System.Windows.Forms.Integration.ElementHost HostdeXAML = new System.Windows.Forms.Integration.ElementHost();
//Declarar o UserControl de XAML
ClassXAML child = new ClassXAML();
HostdeXAML .Child=child;
HostdeXAML.Dock = DockStyle.Fill;
this.Controls.Add(HostdeXAML);

Para integrar Windows Forms em XAML fazemos:
System.Windows.Forms.Integration.WindowsFormsHost HostdeForms = new System.Windows.Forms.Integration.WindowsFormsHost();
ClassForms child = new ClassForms();
HostdeForms.Child=child;
HostdeForms.Margin=new System.Windows.Thickness(0);
HostdeForms.Child = child;
System.Windows.Controls.Grid.SetRow(HostdeForms, 0);
System.Windows.Controls.Grid.SetColumn(HostdeForms, 0);
myGrid.Children.Add(HostdeForms);