//Listview selection changed private void objectListView1_SelectedIndexChanged(object sender, EventArgs e) { if (Thimble == null || Thimble.BundleFiles.Count == 0 || objectListView1.SelectedIndex == -1) { return; } EnableDisableControlsContextDependant(); int index = Thimble.BundleFiles.IndexOf((BundleEntry)objectListView1.SelectedObject); switch (Thimble.BundleFiles[index].FileType) { case BundleEntry.FileTypes.Image: panelImage.BringToFront(); using (MemoryStream ms = new MemoryStream()) { //Release resources from old image var oldImage = pictureBoxPreview.Image as Bitmap; if (oldImage != null) { ((IDisposable)oldImage).Dispose(); } Thimble.SaveFileToStream(index, ms); var image = Image.FromStream(ms); pictureBoxPreview.Image = image; //Set scaling mode depending on whether image is larger or smaller than the picturebox. From https://stackoverflow.com/questions/41188806/fit-image-to-picturebox-if-picturebox-is-smaller-than-picture var imageSize = pictureBoxPreview.Image.Size; var fitSize = pictureBoxPreview.ClientSize; pictureBoxPreview.SizeMode = imageSize.Width > fitSize.Width || imageSize.Height > fitSize.Height ? PictureBoxSizeMode.Zoom : PictureBoxSizeMode.CenterImage; } break; case BundleEntry.FileTypes.Sound: panelAudio.BringToFront(); break; case BundleEntry.FileTypes.Text: panelText.BringToFront(); using (MemoryStream ms = new MemoryStream()) { Thimble.SaveFileToStream(index, ms); string[] tempArray = new string[] { }; //initialise an empty array of strings if (Decoders.ExtractText(ms, out tempArray) == true) { textBoxPreview.Lines = tempArray; } } break; default: panelBlank.BringToFront(); break; } }