Beginner ILNumerics Plotting Sphere example - winforms

failing to follow a begiiner example: create a interactove sphere with ILNumerics. I added the nuget package as reference and draging a ILPanel from the toolbar to my form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ILNumerics;
using ILNumerics.Drawing;
using ILNumerics.Drawing.Plotting;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void ilPanel1_Load_1(object sender, EventArgs e) {
var scene = new ILScene();
scene.Add(new ILSphere());
ilPanel1.Scene = scene;
}
}
}
It shows a sphere. But the sphere always is the full size of the window. Mouse rotation does not work either. What am I missing?

Instead of
scene.Add(new ILSphere());
you can add the sphere below the standard Camera in the scene:
scene.Camera.Add(new ILSphere());
This will give you the desired result. The camera creates its own coordinate system, positions objects within its subtree and provides all interactive options for them (rotation, zoom, pan etc.)

Related

Picturebox and button click

Ran into a confusing problem. I need to make a fishtank in Windows forms. Every time a button is clicked, a fish will appear. I thought about putting code in button_click function. The problem is that the picturebox with the image doesn't appear when I click the button.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
BackColor = System.Drawing.Color.LightBlue;
}
private void button1_Click(object sender, EventArgs e)
{
//PictureBox pb = new System.Windows.Forms.PictureBox();
PictureBox pb = new PictureBox();
pb.Image = Image.FromFile("C:\\Users\\Elonas\\Desktop\\FishTank\\Photo\\Fish_right.png");
pb.Location = new Point(300, 300);
}
}
}
Every time you create a control (pb, in this case), you have to add it to the Controls collection of the form before you can see it.
You can also replace pb.image in the picturebox instead of creating a new picturebox every mouse click. You still need to add it to the form's controls collection when you create it as new (or create it in the Designer).
Ow so something like this? Is this one of the controls? Never was I introduced. I get the idea. Ill try to work with that.
pb.Location = new Point(300, 300);

WPF fade-in window animation using opacy is not very smooth at the first time

I want to display a window using a standard fade-in effect. Everything works well but the first time I load and show the window the fade in animation is not very smooth. If I close and show it again the animation works better.
Any way to improve the fade-in at the first time?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace Kiosk
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
TimeSpan fadeTime = TimeSpan.FromMilliseconds(250);
var fadeInAnimation = new DoubleAnimation(1d, fadeTime);
BeginAnimation(Window.OpacityProperty, fadeInAnimation);
}
}
}

What are some common issues in a winform application for memory leaks?

I try to get to grasp with Ants Memory Profiler. The problem I have is that I dont have an easy application that has a memory leak.
I used the sample of Redgate (QueryBee), but it was to contreived for my taste. There has to be an easier app for that.
I tried to make one up but it is not working. It is not working means: I dont have a memory leak. I read about calling ShowDialog without disposing the called form would get me a memory leak, but thats not the case here.
I use VS2010 and .NET 4.0
I am especially interested in issues that are very common.
Here is what I have so far.Can you get me a memory leak?:
MainForm
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MemoryLeakTest
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SubForm formToCall = new SubForm();
formToCall.ShowDialog();
}
}
}
Subform
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace MemoryLeakTest
{
public partial class SubForm : Form
{
public SubForm()
{
InitializeComponent();
}
private void SubForm_Load(object sender, EventArgs e)
{
ArrayList persons = new ArrayList();
for (int i = 0; i <= 50000; i++)
{
var person = new {
Name = "1 SchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorsch",
LastName = "KluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluni",
Age = 50,
City = "LondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondon",
Zip = "223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301",
Index = i
};
persons.Add(person);
}
dataGridView1.DataSource = persons;
}
private void SubForm_FormClosed(object sender, FormClosedEventArgs e)
{
//this.Dispose();
}
}
}
Add this to your SubForm
public void mouse_handler(object sender, MouseEventArgs e)
{
}
And change the MainForm to do this
private void button1_Click(object sender, EventArgs e)
{
SubForm formToCall = new SubForm();
this.MouseClick += new MouseEventHandler(formToCall.mouse_handler);
formToCall.ShowDialog();
}
Now it doesn't matter if you .Dispose() the SubForm or not, you will still have a "leak". Your MainForm keeps a reference to the SubForms indefinitely, through its mouse event handler, which is basically just a list of who is to receive the events, since that handler is never de-registered.
Ants will help you track down this, but rather manually, it'll show you objects still being alive and referenced from the root, and you have to discover that these objects should not referenced anywhere. I believe Ants also can generate warnings/etc. when it finds objects that have been .Disposed(), but are still referenced somewhere.

screenshot of a winforms control through C#

Is it possible to get a screenshot of a control in winforms without actually displaying it on the screen? How about a webBrowser component?
Yes. This can be done.
I whipped up a sample program to do it. Forgive my naming conventions and code organization, this was whipped up very quickly. You can modify it to better fit your needs, but this shows the basics.
I have a single form with three controls:
button1: Button with the default settings.
button2: Button with the Visible property set to false and the Text set to "button2 - not visible"
webBrowser1: WebBrowser control with the visibility set to false, set the size to 250, 101 (just so it fits on my form. The size is relevant when you look at the capture at the bottom of my answer. You'll need to size it accordingly.)
here's the code in Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PrintInvisibleControl(button2, #"C:\button.jpg");
PrintInvisibleControl(webBrowser1, #"C:\webbrowser.jpg");
}
private void PrintInvisibleControl(Control myControl, string filename)
{
Graphics g = myControl.CreateGraphics();
//new bitmap object to save the image
Bitmap bmp = new Bitmap(myControl.Width, myControl.Height);
//Drawing control to the bitmap
myControl.DrawToBitmap(bmp, new Rectangle(0, 0, myControl.Width, myControl.Height));
bmp.Save(filename, ImageFormat.Jpeg);
bmp.Dispose();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.microsoft.com");
}
}
}
This resulted in the following captures:

Place an Icon Object in WPF

I have instantiated several System.Drawing.Icon Objects. Note that these are created at runtime and are not stored and loaded from the file system. I would like to place these images in my WPF application.
However as I have discovered over the last few hours, it is not possible to simply add an object such as an system.drawing.image or icon directly to the canvas/stack panel, nor is it possible to set the source of a System.Windows.Controls.Image to an image or icon not stored within the file system (or so it seems to me).
Any ideas?
This has worked for me dynamically setting a WPF Image with bytes loaded from a bitmap that was either dynamically generated or loaded from disk:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Drawing.Imaging;
namespace Examples
{
public class Util
{
private static void SetBitmap(Image imgDest, Bitmap bmpSource)
{
byte[] imageBytes;
using (MemoryStream stream = new MemoryStream())
{
bmpSource.Save(stream, ImageFormat.Png);
imageBytes = stream.ToArray();
}
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(imageBytes);
bitmapImage.EndInit();
imgDest.Source = bitmapImage;
}
}
}

Resources