Autosize Button with an image - c#

How to make a buttons auto-size with an image instead of fixing a default button size? Thanks really appreciated.
All my buttons are fixed size, I need to have some buttons buttons to be big, some small depending on the image size.
heres my codes:
Button tba = new Button();
tba.FontSize = 19;
tba.Height = 300
tba.MinWidth = 100;
//tba.Height = Double.NaN;
//tba.Width = Double.NaN;
ImageBrush brush = new ImageBrush();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(#"files.png" + lstQuestion[i].ImageURL.Substring(1), UriKind.Absolute);
bitmap.EndInit();
brush.ImageSource = bitmap;
tba.Background = brush;
wrapPanel1.Children.Add(tba);

To size the button to the image: host the image in an Image and set Image.Strech to None and remove the sizes from your Button:
Button tba = new Button();
Image myImage = new Image();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(#"files.png" + lstQuestion[i].ImageURL.Substring(1), UriKind.Absolute);
bitmap.EndInit();
myImage.Source = myBitmapImage;
myImage.Stretch = Stretch.None;
tba.Content = myImage;
If you want more than just an image in your Button add the image to panel that will let the image fill the space, e.g. a Grid, then set Button.Content to that panel.
Incidentally in XAML you can just go:
<Button>
<Image Strech="None" Source=".\Water Lilies.jpg"/>
</Button>

Related

DockPanel ImageOption.Image is not showing the image

I'll make it short,
My problem is: The image that i want to set in my DockPanel as icon is not showing.
Here is my Code
DockPanel dock = new DockPanel();
dock.ImageOption.Image = <Bitmap image here>; //
dock.Controls.Add(UserControl1);
UserControl1.MyDockPanel = dock;
Any expert opionion here?
Thanks
You can add image to DockPanel in the cs like this:
DockPanel dock = new DockPanel();
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri("...\\ico_48.ico", UriKind.RelativeOrAbsolute);
myBitmapImage.DecodePixelWidth = 2048;
myBitmapImage.EndInit();
Image image = new Image();
image.Source = myBitmapImage;
dock.Children.Add(image);
Content = dock;

How to change foreground color for SVG images in WPF and c#

We are trying to get bitmap from SVG but its not giving the exact co-ordinate to change the pixel color.Can anyone help me how to change the color for bitmap pixel/svg pixel in wpf.
WpfDrawingSettings wpfDrawingSettings = new WpfDrawingSettings { IncludeRuntime = false, TextAsGeometry = false, OptimizePath = true };
StreamSvgConverter converter = new StreamSvgConverter(wpfDrawingSettings);
MemoryStream ms = new MemoryStream();
converter.Convert(str, ms);
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = ms;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();

ImageBrush for Grid programmatically

So I try to apply an image but cannot see any changes...
What do I am missing? Thanks!!
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(#"pack://application:,,,/Images/bg1.jpg", UriKind.RelativeOrAbsolute);
bi.EndInit();
ImageBrush ib = new ImageBrush();
ib.TileMode = TileMode.Tile;
ib.ImageSource = bi;
ib.Stretch = Stretch.None;
RootGrid.Background = ib;
Try this instead:
var ib = new ImageBrush {
ImageSource =
new BitmapImage(
new Uri(#"Images\bg1.jpg", UriKind.Relative)
)
};
RootGrid.Background = ib;
Also, this is obvious, but make sure the image is actually at the right path and set to be Content in the Project.

WPF Add a png image from resources with C# at runtime

I'm trying to add an image in a stack panel at runtime. My image is in the resources of the application. Here's the code that I have for the moment:
Image image = new Image();
ImageSourceConverter isc = new ImageSourceConverter();
image.Source = isc.ConvertFrom(Properties.Resources.entity16_10) as ImageSource;
image.Height = 16;
image.Width = 16;
panel.Children.Add(image);
I've got a null pointer on the line where I'm trying to use the converter, I don't know if it's the good way of doing this.
Here's how i do it :
object imguri = new Uri("/MyAssembly;Component/MyImageFolder/MyImage.png", UriKind.Relative);
BitmapImage ni = new BitmapImage(imguri);
Image img = new Image();
img.Source = ni;
return img;

Image not visible in a custom canvas (derived class of Canvas)

I have a custom canvas in an application, which is not showing image, in XAML, i write:
<local:MyCanvas>
<local:MyCanvas.LayoutTransform>
<ScaleTransform x:Name="scale" ScaleX="1" ScaleY="1" />
</local:MyCanvas.LayoutTransform>
<Image Source="C:\abc.jpg" />
</local:MyCanvas>
I have tried it on Canvas, and it works, but in the derived class, it doesn't appear, but Visual Studio shows the outline meaning image has been added.
As an alternative, inside MyCanvas:Canvas class, i type:
Image img = new Image();
img.Width = 200;
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(#"C:\abc.jpg");
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
img.Source = myBitmapImage;
this.Children.Add(img);
Still not visible. Any ideas?
I have been experimenting with different things and I have been able to add the image by enclosing it in a DrawingVisual, here is the code:
private void addImage (DrawingContext dc)
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(#"C:/abc.jpg");
bi.EndInit();
dc.DrawImage(bi, new Rect(new Point(0,0), new Point(200,200)));
}
And by then adding the DrawingVisual to the custom Canvas, here is the code:
DrawingVisual abc = new DrawingVisual();
DrawingContext dc = abc.RenderOpen ();
addImage (dc)
dc.Close();
base.AddVisualChild(abc);
base.addLogicalChild(abc);
Using this method, the image is visible, and it is working fine.
But why isn't it adding to MyCanvas using the previous method?

Categories