tag:blogger.com,1999:blog-61603326519929564312024-02-08T19:59:34.237+02:00Tan Silliksaar C# BlogTan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-6160332651992956431.post-42952555221873145862009-12-29T08:51:00.004+02:002009-12-29T09:00:18.960+02:00WPF Image and HDCI 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.<br /><br /><pre name="code" class="c-sharp"><br />System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 100);<br />using (var g = System.Drawing.Graphics.FromImage(bmp))<br />{<br /> IntPtr hdc = g.GetHdc();<br /> //use hdc to draw here<br />}<br />var img = new System.Windows.Controls.Image<br />{<br /> Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap<br /> (<br /> bmp.GetHbitmap(), <br /> IntPtr.Zero, <br /> Int32Rect.Empty,<br /> System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()<br /> )<br />};<br />//use img in wpf<br /></pre>Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com1tag:blogger.com,1999:blog-6160332651992956431.post-74423857661642235802009-09-21T19:03:00.003+03:002009-09-21T19:05:07.199+03:00How to scroll Silverlight DataGrid with mouse wheel<pre name="code" class="c-sharp"><br />using System.Windows.Automation.Provider;<br />using System.Windows.Automation;<br /><br />public class ScrollableDataGrid : DataGrid<br />{ <br /> public ScrollableDataGrid()<br /> {<br /> MouseWheel += delegate(object sender, MouseWheelEventArgs e)<br /> {<br /> (OnCreateAutomationPeer() as IScrollProvider).Scroll(ScrollAmount.NoAmount,<br /> e.Delta < 0 ? ScrollAmount.SmallIncrement : ScrollAmount.SmallDecrement);<br /> };<br /> }<br />}<br /></pre>Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com2tag:blogger.com,1999:blog-6160332651992956431.post-13293656546010622142009-08-27T19:00:00.003+03:002009-08-27T19:02:29.791+03:00Why images on canvas appear blurry in Silverlight/WPF?This will happen if their left or top is not a rounded number.<br />All the pixels of the image will be divided between more than one pixel and that makes the images appear blurry.<br />The solution is to use Math.Round when setting their positions.Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com0tag:blogger.com,1999:blog-6160332651992956431.post-31281784850259045172009-08-19T11:05:00.002+03:002009-08-19T11:06:51.638+03:00Getting MapPath in WCFThis only works when the WCF service is running in IIS.<br /><pre name="code" class="c-sharp"><br />System.Web.Hosting.HostingEnvironment.MapPath("file.png");<br /></pre>Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com1tag:blogger.com,1999:blog-6160332651992956431.post-7164179086860431792009-08-13T13:06:00.004+03:002009-08-13T13:14:09.764+03:00Play a seamless loop of video in Silverlight<p><br />How to play a video in a loop in Silverlight?<br />On numerous websites, a solution using MediaEnded event is offered.<br />That solution does not work because there's a visible pause before restart.<br /></p><p><br />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. <br /></p><p><br />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. <br /><pre name="code" class="c-sharp"><br />MediaElement.MediaOpened += delegate<br />{<br /> MediaElement.Markers.Add(new TimelineMarker<br /> {<br /> Time = MediaElement.NaturalDuration.TimeSpan.Subtract(TimeSpan.FromSeconds(0.1))<br /> });<br /> MediaElement.MarkerReached += delegate<br /> {<br /> MediaElement.Stop();<br /> MediaElement.Play();<br /> };<br />};<br /></pre><br />Note that the marker needs to be set after opening the media file.<br /></p>Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com0tag:blogger.com,1999:blog-6160332651992956431.post-1816783307739423682009-08-12T13:28:00.000+03:002009-08-12T13:29:07.853+03:00Scrolling ScrollViewer with Mouse Wheel in Silverlight 3<pre name="code" class="c-sharp"><br />scrollViewer1.MouseWheel += delegate(object sender, MouseWheelEventArgs e)<br />{<br /> scrollViewer1.ScrollToVerticalOffset(scrollViewer1.VerticalOffset - e.Delta);<br />};<br /></pre>Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com2tag:blogger.com,1999:blog-6160332651992956431.post-9902793732747477282009-08-07T19:31:00.001+03:002009-08-07T19:32:43.124+03:00Determine Client's IP Address in WCF<pre name="code" class="c-sharp"><br /> string ClientIP<br /> {<br /> get<br /> {<br /> return (OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty).Address;<br /> }<br /> }<br /></pre>Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com0tag:blogger.com,1999:blog-6160332651992956431.post-15398372472898119542009-07-31T10:27:00.001+03:002009-07-31T10:33:21.314+03:00Adobe Illustrator Plugin for Exporting to XAMLWow, it's free and it works:<br /><a href="http://www.mikeswanson.com/xamlexport/">http://www.mikeswanson.com/xamlexport/</a>Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com0tag:blogger.com,1999:blog-6160332651992956431.post-74670265154936263332009-07-31T09:32:00.001+03:002009-07-31T09:34:35.805+03:00Link to Start a ChatHere's a link to start a Skype chat:<br /><pre><br />skype:tan.silliksaar?chat<br /></pre><br />And here's one to start a Windows Live Messenger (MSN) chat:<br /><pre><br />msnim:chat?contact=tan@rtedev.com<br /></pre>Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com0tag:blogger.com,1999:blog-6160332651992956431.post-69908754872562092492009-07-30T19:03:00.002+03:002009-07-30T19:05:59.556+03:00Drop Shadow on MouseEnter in Silverlight 3Silverlight 3 has made it really simple now.<br />Go explore other effects too, they're all in the same namespace.<br /><pre name="code" class="c-sharp"><br />using System.Windows.Media.Effects;<br /></pre><br /><pre name="code" class="c-sharp"><br />MouseEnter += delegate<br />{<br /> Effect = new DropShadowEffect<br /> {<br /> Color = Colors.Black,<br /> BlurRadius = 13,<br /> ShadowDepth = 3<br /> };<br />};<br />MouseLeave += delegate<br />{<br /> Effect = null;<br />};<br /></pre>Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com0tag:blogger.com,1999:blog-6160332651992956431.post-23214127738798043952009-07-30T18:54:00.002+03:002009-07-30T18:55:56.613+03:00Creating DependencyProperties for a ClassThe following code is needed if you want to animate Padding.Top of a control.<br /><pre name="code" class="c-sharp"><br />public class MyClass : UserControl<br />{<br /> public static readonly DependencyProperty PaddingTopProperty = DependencyProperty.Register("PaddingTop", typeof(double), typeof(MyClass), new PropertyMetadata(0d, new PropertyChangedCallback(OnPaddingTopPropertyChanged)));<br /><br /> public double PaddingTop<br /> {<br /> get<br /> {<br /> return Padding.Top;<br /> }<br /> set<br /> {<br /> Padding = new Thickness(Padding.Left, value, Padding.Right, Padding.Bottom);<br /> }<br /> }<br /><br /> private static void OnPaddingTopPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)<br /> {<br /> (d as MyClass).PaddingTop = (double)e.NewValue;<br /> }<br />}<br /></pre>Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com0tag:blogger.com,1999:blog-6160332651992956431.post-53147651615065430482009-07-29T08:19:00.000+03:002009-07-29T08:20:31.117+03:00How to make Canvas as big as the screen in Silverlight<pre name="code" class="c-sharp"><br />Width = App.Current.Host.Content.ActualWidth;<br />Height = App.Current.Host.Content.ActualHeight;<br /></pre>Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com0tag:blogger.com,1999:blog-6160332651992956431.post-69566400237280719282009-07-20T14:06:00.002+03:002009-07-20T14:11:46.257+03:00System.Security.SecurityException in Silverlight WCFHere's one possible solution to a problem when a Security error is thrown:<br />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.Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com0tag:blogger.com,1999:blog-6160332651992956431.post-2187526687583773382009-07-10T14:58:00.008+03:002009-07-20T13:39:38.687+03:00Minimum Code for Silverlight AnimationLet's change the height of an element.<br /><pre name="code" class="c-sharp"><br /> DoubleAnimation dy = new DoubleAnimation<br /> {<br /> From = from * element.ActualHeight,<br /> To = to * element.ActualHeight,<br /> Duration = TimeSpan.FromSeconds(0.3)<br /> };<br /> Storyboard.SetTarget(dy, element);<br /> Storyboard.SetTargetProperty(dy, new PropertyPath(FrameworkElement.HeightProperty));<br /> Storyboard sb = new Storyboard();<br /> sb.Children.Add(da);<br /> sb.Begin();<br /></pre>Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com0tag:blogger.com,1999:blog-6160332651992956431.post-81008101955019433002009-07-09T12:39:00.003+03:002009-07-20T13:42:58.359+03:00Minimum Code for Silverlight Rotation AnimationThis is the minimum code for animate rotating a Silverlight element (a variable named Element):<br /><pre name="code" class="c-sharp"><br /> Element.RenderTransformOrigin = new Point(0.5, 0.5);<br /> Element.RenderTransform = new RotateTransform();<br /> DoubleAnimation da = new DoubleAnimation<br /> {<br /> From = 0,<br /> To = 360,<br /> Duration = TimeSpan.FromSeconds(0.5)<br /> };<br /> Storyboard.SetTarget(da, Element.RenderTransform);<br /> Storyboard.SetTargetProperty(da, new PropertyPath(RotateTransform.AngleProperty));<br /> Storyboard sb = new Storyboard();<br /> sb.Children.Add(da);<br /> sb.Begin();<br /></pre>Tan Silliksaarhttp://www.blogger.com/profile/04202721543317890055noreply@blogger.com4