STDIO Training

45 bundles
1 file5 months ago
2

C# - Windows Forms App Basic Controls

Code mẫu sử dụng 1 số controls cơ bản của Windows Form App (.NET Framework).

PictureBox

Download 1 ảnh từ url và hiển thị với PictureBox - C#.

123456789101112131415161718192021222324252627282930313233343536373839
private void DownloadImage(string imageUrl, string filename, ImageFormat format) { WebClient client = new WebClient(); Stream stream = client.OpenRead(imageUrl); Bitmap bitmap = new Bitmap(stream); if (bitmap != null) { bitmap.Save(filename, format); } stream.Flush(); stream.Close(); client.Dispose(); } private string getFileNameFromUrl(string link) { Uri uri = new Uri(link); if (uri.IsFile) { return Path.GetFileName(uri.LocalPath); } return null; } private void profilePictureButton_Click(object sender, EventArgs e) { string imageUrl = @"https://api.bugs.vn/static/images/5d8c6888d9bdb1220bf644df/1573696448708_thumbnail.jpg"; string fileName = getFileNameFromUrl(imageUrl); if (fileName == null) fileName = "temp.png"; string filePath = @"./" + fileName; DownloadImage(imageUrl, filePath, ImageFormat.Png); previewPictureBox.Image = new Bitmap(filePath); }