Show an image from a URL in WinForms PictureBox - winforms

Hi I have a WinForms project targetting .net core. I am trying to load an image from a URL, but all I am getting is this:
Inside a UserControl I have added a PictureBox (pic1) and a Button.
Added the following as an example to the click event of the button:
pic1.LoadAsync("https://images.pexels.com/photos/4815143/pexels-photo-4815143.jpeg");
also
pic1.ImageLocation = "https://images.pexels.com/photos/4815143/pexels-photo-4815143.jpeg";
Have tried with different URLs
Any ideas what is wrong?
Edit
I've tried an alternative option, something seems to be getting stuck on DownloadData
using (WebClient webClient = new WebClient())
{
byte[] data = webClient.DownloadData("https://images.pexels.com/photos/4815143/pexels-photo-4815143.jpg");
using (MemoryStream mem = new MemoryStream(data))
{
using (var yourImage = Image.FromStream(mem))
{
}
}
}

Related

WinForms - Show notification count in app launcher Icon

In my WinForms application, I want to display the notifications count in the app launcher icon.
How can this be achieved ?
I believe this is what you're asking for, unfortunately it is in WPF. Winforms doesn't provide a way to do that. You need to P/Invoke manually.
Download Windows 7 API Code Pack - Shell
and use the following.
private void SetTaskBarOverlay()
{
string notificationCount = "3"; //To do: Add this as a parameter
var bmp = new Bitmap(32, 32);
using (Graphics g = Graphics.FromImage(bmp))
{
g.FillEllipse(Brushes.Blue, new Rectangle(Point.Empty, bmp.Size));
g.DrawString(notificationCount, new Font("Sans serif", 25, GraphicsUnit.Point),
Brushes.White, new Rectangle(Point.Empty, bmp.Size));
}
var overlay = Icon.FromHandle(bmp.GetHicon());
TaskbarManager.Instance.SetOverlayIcon(overlay, "");
}
private void RemoveTaskBarOverlay()
{
TaskbarManager.Instance.SetOverlayIcon(null, "");
}
You may alter the painting code to achieve the desired effect.

Listbox default image

In an windows phone 7 application I'm populating one listbox with remote images .. since the images are not downloaded instantly I want to load a default image until the remote image are ready. What is the best way to do this?
Until now, I have the following code skelton:
public partial class RemoteImage : PhoneApplicationPage
{
ObservableCollection<Image> images = new ObservableCollection<Image> { };
public RemoteImage()
{
InitializeComponent();
listImage.ItemsSource = GetAllImages();
}
private ImageSource GetImageSource(string fileName)
{
return new BitmapImage(new Uri(fileName, UriKind.Absolute));
}
private ObservableCollection<Image> GetAllImages()
{
WebClient restClient = new WebClient();
restClient.OpenReadAsync(new Uri(#"http://www.my-api.com"));
restClient.OpenReadCompleted += new OpenReadCompletedEventHandler(onReadComplete);
return images;
}
private void onReadComplete(object sender, OpenReadCompletedEventArgs args)
{
Stream stm = args.Result;
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RootObject));
RootObject ro = (RootObject)ser.ReadObject(stm);
foreach (var item in ro.items)
{
images.Add(new Image{ PhotoSource = GetImageSource(item.image.link) });
}
}
}
If you know, how many images you would need, you should create first the number of default images. Load some image file directly at your project and use it as imageSource for default images. Then, when you'll finish downloading remote images, you should set the new image source for each.
When I got the similar issue, I had some problems with defining which exactly downloaded image refers to which object on page. (As you remember the WebClient objects work asynchronously, so if you have 10 images on page and download 10 remote images at once you can't say that the first downloaded image is the first on page) To solve this you could create more complicated download method (I used a delegate to transfer the id/name of image) or use recursion (Start download method for first image, download it, set source for one on page, download next one...).

passing values between silverlight applications

I have created a Silverlight Business Application which runs as my main silverlight page. For each hyperlink button on my "menu" I launch another Silverlight Application which is created as a different project in Visual Studio. These are non-Business Applications.
Everything is working well. However I'm trying to pass a value from my main SL application to the SL application inside.
I have been googling a lot and cannot find an answer.
As I understand the InitParam is used between ASP and SL, and not between SL apps.
Since the App config is launched for the first SL app and the app config for the second application in never lauched, I'm not able to use that (thats at least my understanding)
The value I want to pass is the login name and role, which is possible to get from webcontext in the Silverlight Business application, but I'm unable to get webcontext in the non-Business application which run inside.
This is how I launch my SL app inside the main SL app:
public Customers()
{
InitializeComponent();
this.Title = ApplicationStrings.CustomersPageTitle;
if (WebContext.Current.User.IsInRole("Users") || WebContext.Current.User.IsInRole("Administrators"))
{
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri("customers.xap", UriKind.Relative));
}
}
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
string appManifest = new StreamReader(Application.GetResourceStream(new StreamResourceInfo(e.Result, null),
new Uri("AppManifest.xaml", UriKind.Relative)).Stream).ReadToEnd();
XElement deploymentRoot = XDocument.Parse(appManifest).Root;
List<XElement> deploymentParts =
(from assemblyParts in deploymentRoot.Elements().Elements() select assemblyParts).ToList();
Assembly asm = null;
AssemblyPart asmPart = new AssemblyPart();
foreach (XElement xElement in deploymentParts)
{
string source = xElement.Attribute("Source").Value;
StreamResourceInfo streamInfo = Application.GetResourceStream(new StreamResourceInfo(e.Result, "application/binary"), new Uri(source, UriKind.Relative));
if (source == "customers.dll")
{
asm = asmPart.Load(streamInfo.Stream);
}
else
{
asmPart.Load(streamInfo.Stream);
}
}
UIElement myData = asm.CreateInstance("customers.MainPage") as UIElement;
stackCustomers.Children.Add(myData);
stackCustomers.UpdateLayout();
}
Anyone?
i agree with ChrisF ,I think that Prism or MEF can resolve you problem.
any way,do some search on the web and look for these two classes:
**
LocalMessageSender
LocalMessageReceiver
**
good luck

How do I load an icon using GetResourceStream in WPF?

I've added an icon to my WPF project (BuildAction set to Resource) and I'm now trying to load that icon into a stream:
using(Stream iconStream = Application.GetResourceStream(new Uri("red.ico")).Stream)
{
// use the stream
}
This gives me the following error:
Invalid URI: The format of the URI could not be determined.
I've tried altering the Uri construction to include UriKind.Relative. This gives:
Cannot locate resource 'red.ico'.
I've looked at various articles on this For Example (from SO) and I can't see what I'm doing wrong.
Any help greatly appreciated.
Try something like new Uri("pack://application:,,,/red.ico"), see this page for more info on URIs in WPF.
Ultimately my issue boiled down to the fact that I had created a custom entry point for my WPF application and I was attempting to create a URI before the Application's static constructions had been called.
The code changed from something like this:
public static void Main()
{
var myUri = new Uri("/red.ico", UriKind.Relative);
var app = new AppMain();
app.Run();
}
to something like this:
public static void Main()
{
var app = new AppMain();
var myUri = new Uri("/red.ico", UriKind.Relative);
app.Run();
}

XAML to XPS memory leak

For a windows service project i have to make reports in xps format. I have xaml code that i turn into an xps document:
private void th_PrintErrorReport(OrderReportData reportData)
{
...
//Use the XAML reader to create a FlowDocument from the XAML string.
FlowDocument document = XamlReader.Load(new XmlTextReader(new StringReader(vRawXaml))) as FlowDocument;
//create xps file
using (XpsDocument xpsDoc = new XpsDocument(vFilePath, System.IO.FileAccess.Write, CompressionOption.Maximum))
{
// create a serialization manager
using (XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false))
{
// retrieve document paginator
DocumentPaginator paginator = ((IDocumentPaginatorSource)document).DocumentPaginator;
// save as XPS
rsm.SaveAsXaml(paginator);
rsm.Commit();
}
}
}
This works but unfortunately creates a memory leak, each report created leaves the wpf controls (contentpresent, labels, etc) in memory. I checked this with a memory profiler. I checked topics like this one and this one which made me think that the wpf dispatcher/message pump is the problem. To make the message pump run i changed my code to:
public void StartHandling()
{
_ReportPrintingActive = true;
//xaml parsing has to run on a STA thread
_ReportPrintThread = new Thread(th_ErrorReportHandling);
_ReportPrintThread.SetApartmentState(ApartmentState.STA);
_ReportPrintThread.Name = "ErrorReportPrinter";
_ReportPrintThread.Start();
}
private void th_ErrorReportHandling()
{
Dispatcher.Run();
}
public void PrintErrorReport(OrderReportData reportData)
{
Action action = () =>
{
th_PrintErrorReport(reportData);
};
Dispatcher.FromThread(_ReportPrintThread).BeginInvoke(action);
}
But still no success. What am i missing ?
Using the reflection code from this post made the memory leak go away : https://stackoverflow.com/a/2410588/687462

Resources