/// <summary> /// ファイルオープン時の処理。 /// </summary> /// <param name="message">ファイルオープンメッセージ。</param> private void OnOpenFileDialog(OpenFileDialogMessage message) { if (!message.DialogResult.HasValue || !message.DialogResult.Value) { return; } try { using (var fs = new FileStream(message.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var reader = new BinaryReader(fs)) { var tga = new TgaImage(reader); ImageSource = tga.GetBitmap(); } OpenedFile = message.FileName; } catch (Exception ex) { MessengerInstance.Send <DialogMessage>( new DialogMessage(string.Format("ファイルのオープンに失敗しました\r\n{0}", ex), (r) => { }) { Caption = "ファイルオープンエラー" }); } }
public static Bitmap OpenTGA(string path) { using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(path))) { //return DmitryBrant.ImageFormats.TgaReader.Load(ms); //Surface surface = Surface.LoadFromStream(ms, true); //return getBitmapFromSurface(surface); using (BinaryReader br = new BinaryReader(ms)) { TgaImage tga = new TgaImage(br); BitmapSource bmpSource = tga.GetBitmap(); return(getBitmapFromBitmapSource(bmpSource)); } } }
public void TestGetBitmap() { var testcase = new[] { new { Input = "TestData/UBW8.tga", Expected = "TestData/grayscale.png", UseAlphaForcefully = false }, new { Input = "TestData/UCM8.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/UTC16.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/UTC24.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/UTC32.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/CBW8.tga", Expected = "TestData/grayscale.png", UseAlphaForcefully = false }, new { Input = "TestData/CCM8.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/CTC16.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/CTC24.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/CTC32.tga", Expected = "TestData/color.png", UseAlphaForcefully = false }, new { Input = "TestData/rgb32rle.tga", Expected = "TestData/rgb32rle.png", UseAlphaForcefully = true }, }; testcase.ToList().ForEach((tc) => { using (var fs = new FileStream(tc.Input, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var r = new BinaryReader(fs)) { var expectedImage = new BitmapImage(new Uri(tc.Expected, UriKind.Relative)); var tga = new TgaImage(r, tc.UseAlphaForcefully); var actualImage = tga.GetBitmap(); var expectedConvertedImage = new FormatConvertedBitmap(expectedImage, PixelFormats.Bgra32, null, 0.0); var bytesPerPixel = (expectedConvertedImage.Format.BitsPerPixel + 7) / 8; var stride = expectedConvertedImage.PixelWidth * bytesPerPixel; var expectedImageBytes = new byte[stride * expectedImage.PixelHeight]; expectedConvertedImage.CopyPixels(expectedImageBytes, stride, 0); var actualConvertedImage = new FormatConvertedBitmap(actualImage, PixelFormats.Bgra32, null, 0.0); var actualImageBytes = new byte[stride * tga.Header.Height]; actualConvertedImage.CopyPixels(actualImageBytes, stride, 0); CollectionAssert.AreEqual(expectedImageBytes, actualImageBytes, string.Format("expected:{0}, actual:{1}", tc.Expected, tc.Input)); } }); }