I can't find the reason why the image won't display in the grid I created
Image img = new Image();
img.Source = new BitmapImage(new Uri(#"C:\logo.bmp", UriKind.Relative));
img.Width = 50;
img.Height = 50;
img.Margin = new Thickness(5, 5, 5, 5);
grid1.Children.Add(img);
Your image URI is an absolute path.
You should write
img.Source = new BitmapImage(new Uri(#"C:\logo.bmp", UriKind.Absolute));
or better
img.Source = new BitmapImage(new Uri(#"C:\logo.bmp"));
Related
I have image file that i wanna crop and load to picturebox. When i download it remotely - all work fine, but when i load it from local machine - picturebox pic stretch...
var img = (Bitmap) Image.FromStream(new WebClient().OpenRead("https://i.snag.gy/MjJca5.jpg"));
img = CropImage(img);
BackgroundImage = img;
BackgroundImageLayout = ImageLayout.Zoom;
public Bitmap CropImage(Image img) {
// create a graphic path to hold the shape data
GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
var points = new Point[] {
new Point(85, 1111), new Point(934, 1111), new Point(934, 952), new Point(1642, 952),
new Point(1642, 2000), new Point(85, 2000), new Point(85, 1111)
};
GraphicsPath gp = new GraphicsPath();
gp.AddPolygon(points.ToArray());
Bitmap bmp1 = new Bitmap(2220, 2220);
using(Graphics G = Graphics.FromImage(bmp1)) {
G.Clip = new Region(gp);
G.DrawImage(img, 0, 0);
bmp1.Save("axi.jpeg");
return bmp1;
}
}
This code return this pic. All work correctly
but this code return this pic.
FileStream file = new FileStream(openFileDialog1.FileName, FileMode.Open);
var img = (Bitmap) Image.FromStream(file);
img = CropImage(img);
pictureBox1.BackgroundImage = img;
pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
...
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>
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.
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;
BitmapImage bitmapImage = new BitmapImage(new Uri("arka_projects_as_logo.png", UriKind.Relative));
Image uiElement = new Image() { Source = bitmapImage };
ScaleTransform t = new ScaleTransform() { ScaleX = 0.2, ScaleY = 0.2 };
WriteableBitmap writeableBitmap = new WriteableBitmap(uiElement,t);
I want to insert the result of this conversions (writeableBitmap) into System.Windows.Controls.Image. When I do this:
Image arkaImage = new Image() { Source = writeableBitmap };
arkaImage isn't shown at all. What can be done to make it work?
WriteableBitmap wb = ..
using (MemoryStream ms = new MemoryStream())
{
wb.SaveJpeg(ms, (int)image1.Width, (int)image1.Height, 0, 100);
BitmapImage bmp = new BitmapImage();
bmp.SetSource(ms);
}
Why don't you just apply the ScaleTransform to the UIElement as well?