Painting change on Graphics not shown unless minimizing window - winforms

I created painting app, where i can paint on Panel, however, the change (what i paint) is shown only when i minimize window (form) and then open it again. I want to see what i paint immediately.
Steps to reproduce:
Panel panel = new Panel();
//adding panel to view
Bitmap bitmap = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bitmap);
panel.BackgroundImage = bitmap;
//painting something on g

Solution was to use PictureBox instead of Panel, Painting on it became visible and more smooth.

Related

With WPF, want to open a second window full screen not in the primary screen

With WPF, want to open a second window full screen. The problem is when the main window is not in the primary screen, the second window doesn't open in the same screen like the main window. It popups inside the primary screen. Is there anyway I can get it open inside the same screen like the main window? Thanks.
You should be able to do this by setting the startup location for the new window to center on its owner. The gotcha to doing that is that you don't get the window it's opened from set as the owner automatically so you need to do it yourself. Doing this from another Window should open the Window2 instance full screen on the same monitor:
Window2 newWindow = new Window2
{
Owner = this,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
WindowState = WindowState.Maximized
};
newWindow.Show();
given window is the instance of the window you want to center
var window = new MyWpfWindow();
var handle = Process.GetCurrentProcess().MainWindowHandle;
var helper = new WindowInteropHelper(window);
helper.Owner = handle;
window.WindowState = WindowState.Maximized;
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
window.ShowDialog();
this will automatically retrieve the current main window and centers and maximize the child in the same screen, it works for me in an Excel VSTO add-ins

WPF Bitmap performance

I'm trying to understand why my images are not snappy, so I built a sample to test WPF performance. I used a timer to calculate how long my "display images" event handler executed, and used a stop watch to measure how long it took the images to appear on the screen. The bottom line: when displaying 100, 1600, 2500 and 3600 images, WPF took 2, 9, 12 and 16 seconds after my code had finished to display the images on the screen. So I feel helpless: It seems I can't improve my code to make the images appear faster - I need to do something with WPF!
So my question is: What do I need to do differently to make the images display faster?
The test setup is simple:
The window contains a Grid. After the "test" button is clicked, row and column definitions are added.Then an Image is added to each cell of the grid as follows:
var image = new Image();
image.BeginInit();
image.Name = ImageNameFromCell(theRow, theColumn);
image.Stretch = Stretch.None;
image.HorizontalAlignment = HorizontalAlignment.Center;
image.VerticalAlignment = VerticalAlignment.Center;
RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.LowQuality);
image.EndInit();
theGrid.Children.Add(image);
Finally, the Source of each image is set to a bitmap:a gray-scale image already scaled down to the estimated screen size. The bitmap is generated as follows:
var smallerBitmapImage = new BitmapImage();
smallerBitmapImage.BeginInit();
smallerBitmapImage.DecodePixelWidth = (int)(theImageWidth);
smallerBitmapImage.UriSource = theUri;
smallerBitmapImage.CacheOption = BitmapCacheOption.None;
smallerBitmapImage.EndInit();
//BitmapFrame bitmapFrame = BitmapFrame.Create(this.FullPath);
var convertedBitmap = new FormatConvertedBitmap();
convertedBitmap.BeginInit();
convertedBitmap.Source = smallerBitmapImage;
convertedBitmap.DestinationFormat = PixelFormats.Gray16;
convertedBitmap.EndInit();
convertedBitmap.Freeze();
So, I'm at my wits end. The images appear with a noticeable delay, and it seems to be out of my control. What can I do?
What appears to have made the difference is setting the image's cache option to OnLoad
smallerBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
This moved the work to my event handler, so now I can use pre-fetching to do this at the background.
Do you actually see all those images at the same time? If not you can use some ItemsControl with a virtualizing panel so only images in view are displayed. (Speaking of panels, your current setup could also be replaced with an ItemsControl which uses a UniformGrid as panel)
You could also try to write a better decoder, which probably is a wasted effort.

How to accelerate WPF fade in/out animation

I implemented lightbox effect with window's opacity change whilst fading in/out. When I have my window maximized this effect has big delay or when I use duration property then opacity change is not smooth.
I manage this eg. with like here:
DoubleAnimation animate = new DoubleAnimation();
animate.From = 1.0;
animate.To = 0.5;
animate.Duration = new Duration(TimeSpan.FromSeconds(0));
this.BeginAnimation(Window.OpacityProperty, animate); // main window
Window1 win = new Window1(); // new window to get focus
win.ShowDialog();
Tell me please, if you know, does this effect works on GPU by default? If not, can I manage this somehow?
The maximization issue sounds like the computer might have performance issues, and the Duration issue exists because you set it to 0, a zero second animation is instant, of course it is not smooth.

How can I hide the cursor in transparent WPF window?

How can I hide the cursor in a WPF window that is fully transparent (alpha=0).
I tried the usual
this.Cursor = System.Windows.Input.Cursors.None;
and it works on areas with content where alpha > 0 but when the cursor moves to an area - in the same window - where the background is fully transparent the cursor re-appears.
I also added
System.Windows.Input.Mouse.OverrideCursor = System.Windows.Input.Cursors.None;
but that didn't help.
I realize that setting the alpha of background to 1 might be a solution but for various reasons this creates other problems...
Maybe as a work-around you can create a tiny non-transparent area somewhere, and move the mouse there just before hiding it:
// Coordinates of your non-transparent area:
var x = 10;
var y = 10;
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(x, y);
this.Cursor = System.Windows.Input.Cursors.None;

How to hide the border around child window

I have a child Window , and I am displaying it from the code behind as below:
ChildPhotoViewer PhotoViewer = new ChildPhotoViewer();
PhotoViewer.DataContext = selectedPhoto;
PhotoViewer.Title = selectedPhoto.strTitle.ToString();
PhotoViewer.Show();
But While Displaying the child window I am getting the Close Button and a Border thickness arround the Window.
I am able to hide the Close Button but is there a way to hide the thickness(Border) across the child window.
Edit:
![alt text][1]
In the Image , there is border arround image after Collpasing the Close button and making
PhotoViewer.Title = null;
PhotoViewer.HasCloseButton = false;
I want to get rid of that Rectangular Border.
Have you tried:-
PhotoViewer.BorderThickness = new Thickness(0);
Edit
Perhaps you are refering to the title block across the top of the window?
PhotoViewer.Title = null;
PhotoViewer.HasCloseButton = false;
Edit
Third attempt.
The template for ChildWindow place the content in border with a 7 pixel margin. This also has an outer border which has a White background. That is what you are seeing in the image. The only way to eliminate it is to copy the ChildWindow template and edit it.
Depends on what you mean by the Border.
If you have a look at the Documentation you can see there is a border (with a thickness of 1) around the edge of the entire window that can be altered like Anthony mentions.
However there is also the window Chrome which in the default template has a number of borders. To change the thickness of these borders you will need to create a style without the borders being present.

Resources