When I download image and display it in an ImageView the image is half corrupted.
BitmapImage bi = new BitmapImage(new Uri(
"http://xxx.xxx.xxx/chatstickerzWinPhone/get_file_from_mysql.php?id="
+ getRandomNumber()));
bi.DownloadProgress += handlerDownloadProgress;
Image i = new Image();
i.Tag = iss + "_random_doodle";
//i.DoubleTap += new EventHandler<System.Windows.Input.GestureEventArgs>(clcoRandomDoodle);
i.Hold += new EventHandler<System.Windows.Input.GestureEventArgs>(clcoRandomDoodle);
i.Width = 440;
i.Height = 300;
//Background = new ImageBrush { ImageSource = bi };
//i.Background = Background;
i.Source = bi;
above is my code to download image.
Use event handler for BitmapImage
public virtual event EventHandler DownloadCompleted
In the handler, use Dispatcher.BeginInvoke() to set the image source of Image.
Related
I cant seem to implement a Drag and Drop Function for video files (.mp4), only images. Everytime I try to drag a video, the program crashed. Here is the drop canvas I had:
private async void mainCanvas_Drop(object sender, DragEventArgs e)
{
Image img = new Image();
img.Width = 200;
img.Height = 150;
BitmapImage bm = new BitmapImage();
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
var storageItems = await e.DataView.GetStorageItemsAsync();
foreach (StorageFile file in storageItems)
{
var stream = await
file.OpenAsync(Windows.Storage.FileAccessMode.Read);
await bm.SetSourceAsync(stream);
img.Source = bm;
}
img.RenderTransform = new CompositeTransform();
img.ManipulationMode = ManipulationModes.All;
img.ManipulationStarted += mPlayer_ManipulationStarted;
img.ManipulationCompleted += mPlayer_ManipulationCompleted;
img.ManipulationDelta += mPlayer_ManipulationDelta;
mainCanvas.Children.Add(img);
Canvas.SetLeft(img, e.GetPosition(mainCanvas).X);
Canvas.SetTop(img, e.GetPosition(mainCanvas).Y);
}
}
You cant set a video as a source to BitmapImage.
BitmapImage bm = new BitmapImage();
await bm.SetSourceAsync(stream);
You need to use MediaPlayerElement to play a video. Have a look at the example.
Edit
I have to delete the drag and drop for image. Is there a way to detect
whether the item Im dropping is image or video?
private async void mainGrid_Drop(object sender, DragEventArgs e)
{
Image img = new Image();
img.Width = 200;
img.Height = 150;
BitmapImage bm = new BitmapImage();
MediaPlayerElement mediaPlayerElement = new MediaPlayerElement();
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
var storageItems = await e.DataView.GetStorageItemsAsync();
foreach (StorageFile file in storageItems)
{
if (file.FileType == ".mp4")
{
mediaPlayerElement.Source = MediaSource.CreateFromStorageFile(file);
mainGrid.Children.Add(mediaPlayerElement);
}
else
{
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
await bm.SetSourceAsync(stream);
img.Source = bm;
mainGrid.Children.Add(img);
}
}
}
}
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;
I use this code to populate WPF Image Control.
string pathToImage = System.IO.Path.Combine(Settings.ContentFolderPath, file);
Image image = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(pathToImage, UriKind.Absolute);
src.EndInit();
double ratio = src.Width / src.Height;
image.Source = src;
image.Stretch = Stretch.Uniform;
image.Height = marquee1.Height;
image.Width = marquee1.Height * ratio;
lstItems.Items.Add(image);
Also I have some parallel Task to update this image file.
But when I try to delete it I am getting the error: File is busy by other process you cannot delete this file.
How to resolve this issue?
Thank you!
UPDATES
So thank you all of you!
The final solution needs to implement
src.CacheOption = BitmapCacheOption.OnLoad;
src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
the working code looks like
Image image = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.CacheOption = BitmapCacheOption.OnLoad;
src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
src.UriSource = new Uri(pathToImage, UriKind.Absolute);
src.EndInit();
double ratio = src.Width / src.Height;
image.Source = src;
image.Stretch = Stretch.Uniform;
image.Height = marquee1.Height;
image.Width = marquee1.Height * ratio;
lstItems.Items.Add(image);
result = image.Width;
MSND BitmapImage.CacheOption says:
Set the CacheOption to BitmapCacheOption.OnLoad if you wish to close a
stream used to create the BitmapImage.
Set the BitmapImage.CacheOption to OnLoad:
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(pathToImage, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
Set the CacheOption property of the BitmapImage to BitmapCacheOption.OnLoad. This will cause it to load the image into memory and close the original file, which should allow you to delete it.
Also I have some parallel Task to update this image file ... when I try to delete it I am getting the error
In general, you can't delete a Windows file while a process is using it. The consumer Windows FAQs include this page to describe this restriction. And as MSDN describes, File.Delete will refuse to delete a file that's in use.