Created
April 19, 2019 01:17
-
-
Save IngIeoAndSpare/1db4a6c65a5c09da334545b020d83a5d to your computer and use it in GitHub Desktop.
DDS image file convert image use marshal copy (use Pfim lib)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// filePath ex) C:\Users\User_name\Desktop\0000.dds | |
private System.Drawing.Image getConvertImageToDDS(string filePath) | |
{ | |
try | |
{ | |
//see https://github.com/nickbabcock/Pfim | |
Pfim.IImage pfImage = Pfim.Pfim.FromFile(filePath); | |
Bitmap pic = new Bitmap(pfImage.Width, pfImage.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); | |
Rectangle rect = new Rectangle(0, 0, pfImage.Width, pfImage.Height); | |
System.Drawing.Imaging.BitmapData bmpData = | |
pic.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, | |
pic.PixelFormat); | |
//You can use the SetPixel method. However, the performance of the Marshal Copy Method is significantly faster. | |
System.Runtime.InteropServices.Marshal.Copy(pfImage.Data, 0, bmpData.Scan0, pfImage.Data.Length); | |
pic.UnlockBits(bmpData); | |
return (System.Drawing.Image)pic; | |
} catch(Exception e) | |
{ | |
// some dds file is not valid. so, I recommend leaving an error log. | |
Console.WriteLine("err file => " + e); | |
Console.WriteLine("err file name=> " + filePath); | |
return Image.FromFile({{SOME_ERROR_SING_IMAGE_FILE_PATH}}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment