Tuesday, December 29, 2009

WPF Image and HDC

I had a DLL exporting a function drawing to a HDC. I needed to use that function in my WPF application. This solution needs a reference to System.Drawing.


System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 100);
using (var g = System.Drawing.Graphics.FromImage(bmp))
{
IntPtr hdc = g.GetHdc();
//use hdc to draw here
}
var img = new System.Windows.Controls.Image
{
Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
(
bmp.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
)
};
//use img in wpf

Monday, September 21, 2009

How to scroll Silverlight DataGrid with mouse wheel


using System.Windows.Automation.Provider;
using System.Windows.Automation;

public class ScrollableDataGrid : DataGrid
{
public ScrollableDataGrid()
{
MouseWheel += delegate(object sender, MouseWheelEventArgs e)
{
(OnCreateAutomationPeer() as IScrollProvider).Scroll(ScrollAmount.NoAmount,
e.Delta < 0 ? ScrollAmount.SmallIncrement : ScrollAmount.SmallDecrement);
};
}
}

Thursday, August 27, 2009

Why images on canvas appear blurry in Silverlight/WPF?

This will happen if their left or top is not a rounded number.
All the pixels of the image will be divided between more than one pixel and that makes the images appear blurry.
The solution is to use Math.Round when setting their positions.

Wednesday, August 19, 2009

Getting MapPath in WCF

This only works when the WCF service is running in IIS.

System.Web.Hosting.HostingEnvironment.MapPath("file.png");

Thursday, August 13, 2009

Play a seamless loop of video in Silverlight


How to play a video in a loop in Silverlight?
On numerous websites, a solution using MediaEnded event is offered.
That solution does not work because there's a visible pause before restart.


Here's one with a Marker. It's still not perfect because if you put the time of the marker too close to the end, it may not be reached. For example, 0.01 seconds did not work.


If you make it too far, the video will not be seamless. But at least, it's better than the MediaEnded way of doing it.


MediaElement.MediaOpened += delegate
{
MediaElement.Markers.Add(new TimelineMarker
{
Time = MediaElement.NaturalDuration.TimeSpan.Subtract(TimeSpan.FromSeconds(0.1))
});
MediaElement.MarkerReached += delegate
{
MediaElement.Stop();
MediaElement.Play();
};
};

Note that the marker needs to be set after opening the media file.

Wednesday, August 12, 2009

Scrolling ScrollViewer with Mouse Wheel in Silverlight 3


scrollViewer1.MouseWheel += delegate(object sender, MouseWheelEventArgs e)
{
scrollViewer1.ScrollToVerticalOffset(scrollViewer1.VerticalOffset - e.Delta);
};

Friday, August 7, 2009

Determine Client's IP Address in WCF


string ClientIP
{
get
{
return (OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty).Address;
}
}

Friday, July 31, 2009

Adobe Illustrator Plugin for Exporting to XAML

Wow, it's free and it works:
http://www.mikeswanson.com/xamlexport/

Link to Start a Chat

Here's a link to start a Skype chat:

skype:tan.silliksaar?chat

And here's one to start a Windows Live Messenger (MSN) chat:

msnim:chat?contact=tan@rtedev.com

Thursday, July 30, 2009

Drop Shadow on MouseEnter in Silverlight 3

Silverlight 3 has made it really simple now.
Go explore other effects too, they're all in the same namespace.

using System.Windows.Media.Effects;


MouseEnter += delegate
{
Effect = new DropShadowEffect
{
Color = Colors.Black,
BlurRadius = 13,
ShadowDepth = 3
};
};
MouseLeave += delegate
{
Effect = null;
};

Creating DependencyProperties for a Class

The following code is needed if you want to animate Padding.Top of a control.

public class MyClass : UserControl
{
public static readonly DependencyProperty PaddingTopProperty = DependencyProperty.Register("PaddingTop", typeof(double), typeof(MyClass), new PropertyMetadata(0d, new PropertyChangedCallback(OnPaddingTopPropertyChanged)));

public double PaddingTop
{
get
{
return Padding.Top;
}
set
{
Padding = new Thickness(Padding.Left, value, Padding.Right, Padding.Bottom);
}
}

private static void OnPaddingTopPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as MyClass).PaddingTop = (double)e.NewValue;
}
}

Wednesday, July 29, 2009

How to make Canvas as big as the screen in Silverlight


Width = App.Current.Host.Content.ActualWidth;
Height = App.Current.Host.Content.ActualHeight;

Monday, July 20, 2009

System.Security.SecurityException in Silverlight WCF

Here's one possible solution to a problem when a Security error is thrown:
Check the ServiceReferences.ClientConfig file in your Silverlight project. When adding a service reference, this file may have got a remote server name inside XML which is not the same as what you use for connecting to the service. This would create a pseudo cross-domain security problem.

Friday, July 10, 2009

Minimum Code for Silverlight Animation

Let's change the height of an element.

DoubleAnimation dy = new DoubleAnimation
{
From = from * element.ActualHeight,
To = to * element.ActualHeight,
Duration = TimeSpan.FromSeconds(0.3)
};
Storyboard.SetTarget(dy, element);
Storyboard.SetTargetProperty(dy, new PropertyPath(FrameworkElement.HeightProperty));
Storyboard sb = new Storyboard();
sb.Children.Add(da);
sb.Begin();

Thursday, July 9, 2009

Minimum Code for Silverlight Rotation Animation

This is the minimum code for animate rotating a Silverlight element (a variable named Element):

Element.RenderTransformOrigin = new Point(0.5, 0.5);
Element.RenderTransform = new RotateTransform();
DoubleAnimation da = new DoubleAnimation
{
From = 0,
To = 360,
Duration = TimeSpan.FromSeconds(0.5)
};
Storyboard.SetTarget(da, Element.RenderTransform);
Storyboard.SetTargetProperty(da, new PropertyPath(RotateTransform.AngleProperty));
Storyboard sb = new Storyboard();
sb.Children.Add(da);
sb.Begin();