private void DdsToBitmap_Click(object sender, RoutedEventArgs e) { //check if it's actually a dds file bool isItADDSFile = DDSFile.IsDDSFile("damageIndicator.dds"); Bitmap bmp = null; //helpful links //https://docs.microsoft.com/en-us/windows/desktop/api/dxgiformat/ne-dxgiformat-dxgi_format#dxgi-format-bc3-unorm //https://docs.microsoft.com/en-us/windows/desktop/direct3d11/texture-block-compression-in-direct3d-11#bc1-bc2-and-b3-formats //best method found to use mode 1 and direct=true bool directBitmap = true; //TEXIMP DDSFILE //this is an Importer and says nothing about bitmap objects. i don't think it's designed to do this (load directly and make bitmap out of) //TEXIMP SURFACE //https://bitbucket.org/Starnick/teximpnet/src/acf2d0a8d7f6?at=master //format of image is Rgba32 using (Surface surface = Surface.LoadFromFile("damageIndicator.dds", ImageLoadFlags.Default)) { surface.FlipVertically(); if (directBitmap) { //https://stackoverflow.com/questions/16478449/convert-intptr-to-bitmapimage bmp = new Bitmap(surface.Width, surface.Height, surface.Pitch, System.Drawing.Imaging.PixelFormat.Format32bppArgb, surface.DataPtr); } else { //stride is rowpitch //length of array is height * stride/pitch int size = surface.Height * surface.Pitch; byte[] managedArrayy = new byte[size]; //https://stackoverflow.com/questions/5486938/c-sharp-how-to-get-byte-from-intptr //https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.copy?view=netframework-4.8#System_Runtime_InteropServices_Marshal_Copy_System_IntPtr_System_Byte___System_Int32_System_Int32_ Marshal.Copy(surface.DataPtr, managedArrayy, 0, size); BitmapSource source = BitmapSource.Create(surface.Width, surface.Height, 96.0, 96.0, PixelFormats.Bgra32, null, managedArrayy, surface.Pitch); bmp = BitmapFromSource(source); //TestImageDisplay.Source = source; } bmp.Save("damageIndicator.png", System.Drawing.Imaging.ImageFormat.Png); bmp.Dispose(); } }
private void ModdedTexOpen() { moddedTexPathBox.Text = moddedTexPath; moddedTexOpen = true; if (!isWebImage) { texturePreview2.Image = null; } moddedFormat = "None"; // Check if file is dds or png if (DDSFile.IsDDSFile(moddedTexPath) == true) { // Get dds data DDSContainer moddedTexture = DDSFile.Read(moddedTexPath); moddedFormat = moddedTexture.Format.ToString(); moddedDDS.MipMaps = moddedTexture.MipChains[0].Count; if (moddedFormat == "BC3_UNorm") { moddedFormat = "DXT5"; } else if (moddedFormat == "BC1_UNorm") { moddedFormat = "DXT1"; } moddedDDS.Format = moddedFormat; // Convert to png for preview DDSImage modDDS = new DDSImage(moddedTexPath); MemoryStream pngStream = new MemoryStream(); modDDS.SaveAsPng(pngStream); var newPNG = Image.FromStream(pngStream); // Change labels moddedTexCompression.Text = moddedDDS.Format; mipMapCountLabel2.Text = moddedDDS.MipMaps.ToString(); texturePreview2.Image = newPNG; previewLabel2.Text = "Preview:"; moddedDDS.ResX = texturePreview2.Image.Width; moddedDDS.ResY = texturePreview2.Image.Height; resolutionCheck2.Text = moddedDDS.ResX.ToString() + "x" + moddedDDS.ResY.ToString(); texturePreview2.SizeMode = PictureBoxSizeMode.Zoom; // Dispose pngStream.Dispose(); moddedTexture.Dispose(); } else { // Bitmap/Png format if (moddedTexPath.Contains(".bmp")) { moddedTexCompression.Text = "None (Bitmap)"; } else if (moddedTexPath.Contains(".png")) { moddedTexCompression.Text = "None (PNG)"; } else { moddedTexCompression.Text = "None"; } mipMapCountLabel2.Text = "None"; previewLabel2.Text = "Preview:"; if (!isWebImage) { texturePreview2.Image = new Bitmap(moddedTexPath); texturePreview2.SizeMode = PictureBoxSizeMode.Zoom; } resolutionCheck2.Text = texturePreview2.Image.Width.ToString() + "x" + texturePreview2.Image.Height.ToString(); } EnableButtons(); }