Position of BackgroundImage in Windows Form - winforms

I am setting the BackgroundImage of a Windows Form to a 200 x 200 image. The Form is 500 x 500. I want the image to be anchored in the bottom right corner of the form. However the only option available to me is the BackgroundImageLayout property - setting this to 'None' results in the image being anchored to the top left. How can I change this?
Note: I am using .NET 2.0

Just draw it yourself in the OnPaintBackground() method. Add the image to the resources (I called it BkgImage) and make the form code look like this:
public Form1() {
InitializeComponent();
backgroundImage = Properties.Resources.BkgImage;
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
private Image backgroundImage;
protected override void OnPaintBackground(PaintEventArgs e) {
base.OnPaintBackground(e);
var rc = new Rectangle(this.ClientSize.Width - backgroundImage.Width,
this.ClientSize.Height - backgroundImage.Height,
backgroundImage.Width, backgroundImage.Height);
e.Graphics.DrawImage(backgroundImage, rc);
}

You cannot do that with the BackgroundImageLayout.
However what you could do is add a PictureBox, anchor it to the bottom right and set it to the lowest z-value. This would result in pretty much the requested effect.

Related

Checkbox Size Xamarin Android

I've implemented checkboxes in my Xamarin Forms App using the following article:
https://alexdunn.org/2018/04/10/xamarin-tip-build-your-own-checkbox-in-xamarin-forms/
The only issue I have is that I can't set the size of Android, there is a question in the comments section, however there is no solution. No matter what I do the SizeRequest is always 64x64 - can anyone offer any suggestions or reason why I can't resize?
Did you try to use before the line 91, the code below in order to scale the control:
Control.ScaleX = 0.70;
Control.ScaleY = 0.70;
Maybe I found a possible solution, check the steps below:
Add a BindableProperty called SizeRequest in the custom CheckBox control.
Create a method GetDefaultCheckBoxDrawable to get the default CheckBox drawable.
Change OnElementChanged method to clear and resize the text, set the width/height based on SizeRequest, reset the button drawable and set a new Background drawable with the default checkbox drawable.
AndroidCheckboxRenderer.cs:
private Drawable GetDefaultCheckBoxDrawable(Android.Views.View view)
{
TypedValue value = new TypedValue();
view.Context.Theme.ResolveAttribute(Android.Resource.Attribute.ListChoiceIndicatorMultiple, value, true);
var origImg = view.Context.Resources.GetDrawable(value.ResourceId);
var porterDuffColor = new Android.Graphics.PorterDuffColorFilter(Element.CheckColor.ToAndroid(), Android.Graphics.PorterDuff.Mode.SrcIn);
origImg.SetColorFilter(porterDuffColor);
return origImg;
}
protected override void OnElementChanged(ElementChangedEventArgs<CustomCheckbox> e)
{
...
// CheckBox displays its height from the TEXT, as well as images.
checkBox.Text = "";
checkBox.SetTextSize(Android.Util.ComplexUnitType.Sp, 0);
// Set the width and height based on SizeRequest
if (Element.SizeRequest >= 0)
{
checkBox.SetWidth((int)Element.SizeRequest);
checkBox.SetHeight((int)Element.SizeRequest);
}
// Reset the Button Drawable
checkBox.SetButtonDrawable(null);
// Set Background Drawable with the default CheckBox
checkBox.SetBackgroundDrawable(GetDefaultCheckBoxDrawable(this));
...
}
Check out my GitHub for the full solution.
I hope this can help you.

windows forms application - how to make the tabs in a tab control get full width?

So i'am working with tabControl in windows forms application and i want to make the tabs get full width regardless whether the application window is maximized or not.
When the window isn't maximized everything appears great:
But when the window gets maximized the tabs doesn't get the full width:
Is there any known way to fix this problem?
Thanks in advance
You can achieve this in some way by modifying the ItemSize property as described bellow, else you'd have to draw the tab page selectors yourself.
public Form1()
{
InitializeComponent();
tabControl1.SizeMode = TabSizeMode.Fixed;
tabControl1.ItemSize = new Size((tabControl1.Width / tabControl1.TabPages.Count) - 1, tabControl1.ItemSize.Height);
}
//Hook to form or parent container Resize event, either Resize or ResizeEnd.
private void Form1_ResizeEnd(object sender, EventArgs e)
{
tabControl1.ItemSize = new Size((tabControl1.Width / tabControl1.TabPages.Count) - 1, tabControl1.ItemSize.Height);
}

Show default local image while url image is loading

I am a wpf newbie. In my wpf app, I have images that load from the web. To avoid gui blocking, I followed the approach as mentioned in How can I keep a WPF Image from blocking if the ImageSource references an unreachable Url?
Works great. Issue is, until the image loads, gui doesn't honor dimensions properties set in the Image node. End result is kind of a 'resize' affect - initially the gui(elements) are of one size and 'readjust' once image is loaded.
I wish to make the load 'smooth'. I want to be able to specify a 'initial default' image. Something like initial image in WPF Image Control
However I am not able to get it to work. May be something like below in ImageAsyncHelper(which is obviously wrong) :
public static readonly DependencyProperty SourceUriProperty = DependencyProperty.RegisterAttached("SourceUri", typeof(Uri), typeof(ImageAsyncHelper), new PropertyMetadata
{
PropertyChangedCallback = (obj,e) =>
{
((Image)obj).SetBinding(Image.SourceProperty,
new Binding("DefaultUri")
{
Source = new Uri("pack://application:,,,/blah/Images/Default.gif", UriKind.RelativeOrAbsolute)
});
}
PropertyChangedCallback += (obj, e) =>
{
((Image)obj).SetBinding(Image.SourceProperty,
new Binding("VerifiedUri")
{
Source = new ImageAsyncHelper { GivenUri = (Uri)e.NewValue },
IsAsync = true,
});
}
});
What are my options?
Try setting the Image elements MinWidth and MaxWidth to the desired size.

Blank - Black Image control in WPF if image source is absolute Uri

I use on listbox control own datatemplate. Listbox item consist one image control and some textblock.
On image source I bind property type of Uri (absolute url - for example: http://u.aimg.sk/fotky/1730/71/17307141.jpg?v=2)
Listbox have about 50 - 300 items.
If I test app, I sometimes see blank - white or black image instead user images.
The problem you can see on this images:
I would like to know what cause this problem and how can I solve this problem.
Image sources are good, I check it in browser.
Thank for advice.
I think what's happening is a race condition. Some of your images haven't completed downloading by the time you are asking them to display. There is a pretty good example given here http://social.msdn.microsoft.com/Forums/en/wpf/thread/dc4d6aa9-299f-4ee8-8cd4-27a21ccfc4d0 which I'll sum up:
private ImageSource _Src;
public ImageSource Src
{
get { return _Src; }
set
{
_Src = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Src"));
((BitmapImage)_Src).DownloadCompleted += new EventHandler(MainWindow_DownloadCompleted);
}
}
void MainWindow_DownloadCompleted(object sender, EventArgs e)
{
PropertyChanged(this, new PropertyChangedEventArgs("Src"));
((BitmapImage)_Src).DownloadCompleted -= MainWindow_DownloadCompleted;
}
With the above code, your images that are binding to your property will be told to update with the PropertyChanged call when the value is first assigned as well as AFTER the images have downloaded 100%. This is taken care of in the DownloadCompleted event handler that is utilized in the above example. This should make them not appear as a black image anymore, but as their fully-ready selves.
Also, if you are using a stream to as the source for your images, you need to make sure you use BitmapCacheOption.OnLoad. Such as:
BitmapImage source = new BitmapImage();
source.BeginInit();
source.CacheOption = BitmapCacheOption.OnLoad;
source.StreamSource = yourStream;
source.EndInit();
This is because by default the image using the source will lazy load it and by then your stream is probably closed, which could also be why you get blank/black images.
Good luck.

Black background before loading a wpf controll when using ElementHost

I'm using WPF in WinForms with ElementHost. When the form loads, there is a flash of black background where the ElementHost is about to load. This looks kind of bad. Any suggestions on how to get rid of this?
Hide the element (Visibility = Hidden) until the WinForms control is fully loaded...
I know this has already been answered and the question is old but none of the presented answers worked for myself and after a long time of troubleshooting the issue. I finally found an easier answer.
If you build a class extending from Element Host and in the initial constructor. You can set a Load Event for the Host Container. The Host Container is the panel that the Element Hosts Child is being displayed on top of. From there, just set the Host Containers background color to being of the Element Hosts Parents background color.
Like this
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
public class MyElementHost : ElementHost
{
public MyElementHost()
{
this.HostContainer.Loaded += new RoutedEventHandler(HostPanelLoad);
}
public void HostPanelLoad(object sender, RoutedEventArgs e)
{
System.Drawing.Color parentColor = this.Parent.BackColor;
this.HostContainer.Background = new SolidColorBrush(Color.FromArgb(parentColor.A, parentColor.R, parentColor.G, parentColor.B));
}
}
you need first show control with empty bounds first time to avoid black flickering
if (!_control.Created && _control.BackColor != Color.Transparent)
{
_control.Bounds = Rectangle.Empty;
_control.Show();
}
// set control bounds and show it
Rectangle bounds = GetBounds(context, rect);
if (_control.Bounds != bounds)
_control.Bounds = bounds;
if (!_control.Visible)
_control.Show();

Resources