Attaching an image to an Ellipse programmatically - c#

When I run this code, I get a black screen until i maximise the application? Also, I don't think its picking up the image file neither. Within Visual Studio, I made a new folder and added the image to this folder.
public MainWindow()
{
InitializeComponent();
Canvas canvas = new Canvas();
canvas.Width = 300;
canvas.Height = 300;
canvas1.Children.Add(canvas);
Ellipse hand = new Ellipse();
hand.Height = 30;
hand.Width = 30;
/*
BrushConverter bc = new BrushConverter();
Brush brush = (Brush)bc.ConvertFrom("Red");
hand.Fill = new SolidColorBrush(Colors.Red);
*/
ImageBrush myBrush = new ImageBrush();
myBrush.ImageSource =
new BitmapImage(new Uri(#"Images/Hand.png", UriKind.Relative));
hand.Fill = myBrush;
Canvas.SetLeft(hand, 100);
Canvas.SetTop(hand, 100);
canvas.Children.Add(hand);
}

Is there any particular reason that you are using TextureBrush?
Not really sure but maybe you should be using ImageBrush instead.
ImageBrush myBrush = new ImageBrush();
myBrush.ImageSource =
new BitmapImage(new Uri("pack://application:,,,/Images/image.jpg"));
myEllipse.Fill = myBrush;

Related

My code complies but doesn't show the ellipse in C#

So I am trying to make a red ellipse flash in the middle but for some reason my codes runs and complies but no ellipse is drawn
As you can see in the code part I have put the drawing part in the default constructor, I have also tried creating a private function that draws it and tried calling it in the default constructor but that didn't work either
using System;
using System.Drawing;
using System.Windows.Forms;
public class RedLuserinterface : Form
{
private Panel panelTop = new Panel();
private Panel panelMid = new Panel();
private Panel panelBot = new Panel();
private Label title = new Label();
private Button pauseBut = new Button();
private Button resumeBut = new Button();
private Button exitBut = new Button();
private Size minInterfaceSize = new Size(400, 600);
private Size maxInterfaceSize = new Size(400, 600);
public RedLuserinterface()
{ //Set the size of the user interface box.
MaximumSize = minInterfaceSize;
MinimumSize = maxInterfaceSize;
//Initialize text strings
Text = "Red Light Assignment";
title.Text = "Red Light Program";
pauseBut.Text = "Pause";
resumeBut.Text = "Resume";
exitBut.Text = "Exit";
//Set Sizes
Size = new Size(400, 600);
panelTop.Size = new Size(400, 30);
panelMid.Size = new Size(400, 160);
panelBot.Size = new Size(400, 50);
title.Size = new Size(120, 30);
pauseBut.Size = new Size(85, 30);
resumeBut.Size = new Size(85, 30);
exitBut.Size = new Size(85, 30);
//Set Locations
title.Location = new Point(140, 20);
panelTop.Location = new Point(0, 0);
panelMid.Location = new Point(0, 30);
panelBot.Location = new Point(0, 480);
pauseBut.Location = new Point(50, 500);
resumeBut.Location = new Point(40, 150);
exitBut.Location = new Point(250, 500);
//Add controls to the form
Controls.Add(title);
Controls.Add(panelTop);
Controls.Add(panelMid);
Controls.Add(panelBot);
Controls.Add(pauseBut);
Controls.Add(resumeBut);
Controls.Add(exitBut);
//Set Color
panelTop.BackColor = Color.Green;
panelMid.BackColor = Color.Blue;
panelBot.BackColor = Color.Yellow;
//Create solid brush and draw ellipse
SolidBrush redBrush = new SolidBrush(Color.Red);
Graphics circle = this.CreateGraphics();
circle.FillEllipse(redBrush, 0, 0, 200, 200);
//send some stuff to the back
panelTop.SendToBack();
panelMid.SendToBack();
panelBot.SendToBack();
pauseBut.Enabled = true;
resumeBut.Enabled = false;
exitBut.Click += new EventHandler(stoprun);
//dispose stuff
redBrush.Dispose();
circle.Dispose();
}
}
You need to paint your ellipse when the Form engine requires you to do it. The form engine will call the Paint event handler if you define one and will pass the Graphics object to use to paint the ellipse. So you should remove the lines in the form constructor and add the proper delegate for the Paint event, then in the Paint event draw the ellipse
public RedLuserinterface()
{
.....
// Remove these lines
// SolidBrush redBrush = new SolidBrush(Color.Red);
// Graphics circle = this.CreateGraphics();
// circle.FillEllipse(redBrush, 0, 0, 200, 200)
....
exitBut.Click += new EventHandler(stoprun);
this.Paint += onFormPaint;
// No more needed here
// redBrush.Dispose();
// circle.Dispose()
}
private void onFormPain(object sender, PaintEventArgs e)
{
SolidBrush redBrush = new SolidBrush(Color.Red);
e.Graphics.FillEllipse(redBrush, 50,250, 200, 200);
redBrush.Dispose();
}

C# tinting an ImageBrush

Is it somehow possible to change the hue of an imagebrush. I'm currently using an imagebrush to create a texture material in wpf 3d. To get selection and grouping effects, i need up to three materials per model. This results in quite the performance hit. Here is the current code:
DiffuseMaterial itemImageMaterial = new DiffuseMaterial(new ImageBrush(ItemTextures[nr]) { Stretch = Stretch.Fill });
var meshGeom = new MeshGeometry3D();
meshGeom.Positions = cubesPositions;
meshGeom.TriangleIndices = cubesIndeces;
GeometryModel3D geoMod = new GeometryModel3D();
geoMod.Geometry = meshGeom;
MaterialGroup mg = new MaterialGroup();
mg.Children.Add(itemImageMaterial);
if (applyGroupOverlay) mg.Children.Add(groupMaterials[groupNr]);
if (applyHighlight) mg.Children.Add(highlightMaterials[highlightNr]);
mg.Freeze();
geoMod.Material = mg;
(geoMod.Geometry as MeshGeometry3D).TextureCoordinates = cubesTextureCoords;
Visual3DModel = geoMod;
nb. ItemTextures[nr] is a freezed BitmapImage
Hopefully there is a solution so that the code can look something like this:
ImageBrush brush = new ImageBrush(ItemTextures[nr]) { Stretch = Stretch.Fill }
if (applyGroupOverlay){
//tint brush here
}
if (applyHighlight) {
//tint brush some more here
}
DiffuseMaterial itemImageMaterial = new DiffuseMaterial(brush);
var meshGeom = new MeshGeometry3D();
meshGeom.Positions = cubesPositions;
meshGeom.TriangleIndices = cubesIndeces;
GeometryModel3D geoMod = new GeometryModel3D();
geoMod.Geometry = meshGeom;
geoMod.Material = itemImageMaterial;
(geoMod.Geometry as MeshGeometry3D).TextureCoordinates = cubesTextureCoords;
This way, only a single material is needed to create the desired effects.

WPF custom effect animation programmatically

I try to use pixel shader effect animation programmatically in WPF, but below code uses a lot of CPU power.
Is there other method?
â– Call Method
Setting effect while doing sequence.
private void Button_Click(object sender, RoutedEventArgs e)
{
string path = System.IO.Path.GetFullPath(#"WateryEffect.ps");
var uri = new Uri(path);
WpfEffect wpfeffect = new WpfEffect(uri);
BitmapImage img1 = new BitmapImage(new Uri(System.IO.Path.GetFullPath(#"3a28168e68.jpg")));
ImageBrush imgbrush1 = new ImageBrush(img1);
wpfeffect.input1 = imgbrush1;
VisualBrush vbrush = new VisualBrush(text1);
wpfeffect.input1 = vbrush;
BitmapImage img2 = new BitmapImage(new Uri(System.IO.Path.GetFullPath(#"e98f58fa-2e58-4d38-9949-f3f40b6a2b13_7.jpg")));
ImageBrush imgbrush2 = new ImageBrush(img2);
wpfeffect.input2 = imgbrush2;
double i = 1;
do
{
wpfeffect.para0 = i;
image1.Effect = wpfeffect;
image2.Effect = wpfeffect;
image3.Effect = wpfeffect;
image4.Effect = wpfeffect;
DoEvents();
i = i - 0.0001;
} while (i > -1);
}
Doing a loop for this goes against the whole advantage of WPF. You need to run this effect update as a DoubleAnimation. I don't have your .ps file but I'm assuming that para01 is a DependencyProperty.
Try this code:
DoubleAnimation da =
new DoubleAnimation(0.0, 1.0, new Duration(new TimeSpan(0,0,1)); // adjust as needed
(image1.Effect as WpfEffect).BeginAnimation(WpfEffect.para01Property, da);
(image2.Effect as WpfEffect).BeginAnimation(WpfEffect.para01Property, da);
(image3.Effect as WpfEffect).BeginAnimation(WpfEffect.para01Property, da);
(image4.Effect as WpfEffect).BeginAnimation(WpfEffect.para01Property, da);

How do I draw polygon?( in C# WPF project)

Click the points, I want to make from the polygon area on image.
myPolygon = new Polygon();
myPolygon.Stroke = Brushes.Black;
myPolygon.Fill = Brushes.LightYellow;
myPolygon.StrokeThickness = 2;
myPolygon.HorizontalAlignment = HorizontalAlignment.Left;
myPolygon.VerticalAlignment = VerticalAlignment.Center;
myPolygon.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(Polygon_MouseDown);
myPolygon.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(Polygon_MouseUp);
private void Polygon_MouseDown(object sender, MouseButtonEventArgs e)
{
Point p = e.GetPosition(image);
myPolygon.Points = new PointCollection() { new Point(p.X,p.Y) };
RootCanvas.Children.Add(myPolygon);
} //MouseClick Event BUT, did not click behavior.. I want draw a line along the points.
How can I do...?
We can draw Polygon using WPF canvas which is a collection of children objects.
Polygon p = new Polygon();
p.Stroke = Brushes.Black;
p.Fill = Brushes.LightBlue;
p.StrokeThickness = 1;
p.HorizontalAlignment = HorizontalAlignment.Left;
p.VerticalAlignment = VerticalAlignment.Center;
p.Points = new PointCollection() { new Point(10, 10), new Point(100, 100), new Point(200, 200) };
freeCanvas.Children.Add(p);
For more information,. please refer the following urls
http://www.codeproject.com/Articles/128705/WPF-rounded-corners-polygon
http://classicalprogrammer.wikidot.com/draw-dynamic-polygons-in-wpf
http://msdn.microsoft.com/en-us/library/ms747393.aspx

Show image above SMFPlayer issue

An image (Image class) is placed above a SMFPlayer (both elements are created in code-behind). Z-index of the image is the Z-Index of SMFPlayer + 1. The image is resized (adjusting the width) according to the playing progress of SMFPlayer.
videoPlayer = new SMFPlayer();
videoPlayer.Width = 1920;
videoPlayer.Height = 1080;
videoPlayer.Margin = new Thickness(1920, 0, 0, 0);
PlaylistItem item = new PlaylistItem();
Random r = new Random();
item.MediaSource = new Uri("video.wmv");
item.DeliveryMethod = DeliveryMethods.ProgressiveDownload;
videoPlayer.Playlist.Add(item);
videoPlayer.AutoPlay = true;
videoPlayer.AutoLoad = true;
videoPlayer.IsControlStripVisible = false;
videoPlayer.PlaylistVisibility = FeatureVisibility.Disabled;
videoPlayer.MediaEnded += new EventHandler(player_MediaEnded);
LayoutRoot.Children.Add(videoPlayer);
bar_yellow3 = new Image();
bar_yellow3.Source = new BitmapImage(new Uri("/SMF_ProgressiveDownload1;component/assets/bar_y.png", UriKind.Relative));
bar_yellow3.Width = 775;
bar_yellow3.Height = 34;
bar_yellow3.Margin = new Thickness(2948,1034,0,0);
bar_yellow3.Stretch = Stretch.Fill;
bar_yellow3.VerticalAlignment = VerticalAlignment.Top;
bar_yellow3.HorizontalAlignment = HorizontalAlignment.Left;
LayoutRoot.Children.Add(bar_yellow3);
However, when the playing progress is less than 20%, the image blinks randomly. When the SMFPlayer is set to be invisible ( Visibility.Collapsed ) , the image is normal.
I have tried to call the update function of the Image, which is: bar_yellow3.UpdateLayout(); but the method does not solve the blinking issue.
Any solution?
Try use effects (Shazzam will help you) instead using Z order.

Categories