|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Configuration; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace ImageWizHelperCS |
| 11 | +{ |
| 12 | + public enum Layout |
| 13 | + { |
| 14 | + A0, |
| 15 | + A1, |
| 16 | + A2, |
| 17 | + A3, |
| 18 | + A4, |
| 19 | + A5, |
| 20 | + A6, |
| 21 | + A7 |
| 22 | + }; |
| 23 | + |
| 24 | + public enum ImageDPI |
| 25 | + { |
| 26 | + DPI_100 = 100, |
| 27 | + DPI_150 = 150, |
| 28 | + DPI_200 = 200, |
| 29 | + DPI_300 = 300, |
| 30 | + DPI_500 = 500, |
| 31 | + DPI_600 = 600 |
| 32 | + }; |
| 33 | + |
| 34 | + public enum ResetOption |
| 35 | + { |
| 36 | + No_DPI_change, // DPI will not be resetted this case . |
| 37 | + ResetAllDPI, // Every image DPI will be resetted to selected DPI. Dimension also will be changed according to DPI |
| 38 | + ResetNonDPI // If DPI is not available then DPI will setted for the image. Dimension also will be changed according to DPI |
| 39 | + }; |
| 40 | + |
| 41 | + public enum ImageQuality |
| 42 | + { |
| 43 | + unknown = -1, |
| 44 | + Photo_Quality = 70, |
| 45 | + Document_Quality = 40, |
| 46 | + Compressed_Document = 20 |
| 47 | + }; |
| 48 | + |
| 49 | + [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)] |
| 50 | + public struct RECT |
| 51 | + { |
| 52 | + public Int32 left; |
| 53 | + public Int32 top; |
| 54 | + public Int32 right; |
| 55 | + public Int32 bottom; |
| 56 | + } |
| 57 | + |
| 58 | + public enum ConversionType |
| 59 | + { |
| 60 | + CONVERSIONTYPE_NO_CONVERSION = 0, |
| 61 | + CONVERSIONTYPE_CONVERT_TO_BW = 1, |
| 62 | + CONVERSIONTYPE_CONVERT_TO_GREY = 2 |
| 63 | + } |
| 64 | + |
| 65 | + public class ImageWizHelper |
| 66 | + { |
| 67 | + const string DLL_NAME = "ImageWizHelper.dll"; |
| 68 | + |
| 69 | + [DllImport("ImageWizHelper.dll", SetLastError = true, CharSet = CharSet.Ansi)] |
| 70 | + static extern IntPtr Initialize(String LogFileName); |
| 71 | + |
| 72 | + [DllImport("ImageWizHelper.dll", SetLastError = true, CharSet = CharSet.Ansi)] |
| 73 | + static extern Int32 Terminate(IntPtr ImageWizHelper); |
| 74 | + |
| 75 | + [DllImport("ImageWizHelper.dll", SetLastError = true, CharSet = CharSet.Ansi)] |
| 76 | + static extern Int32 GetLastErrorCode(); |
| 77 | + |
| 78 | + [DllImport("ImageWizHelper.dll", SetLastError = true, CharSet = CharSet.Ansi)] |
| 79 | + static extern Int32 GetErrorDescription(Int32 error, StringBuilder desc_buffer, ref Int32 desc_len);//StringBuilder desc_buffer |
| 80 | + |
| 81 | + |
| 82 | + [DllImport("ImageWizHelper.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 83 | + static extern Int32 UnlockLibraryWithModuleIdentity(String licPath, StringBuilder err_buff); |
| 84 | + |
| 85 | + [DllImport("ImageWizHelper.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 86 | + private static extern Int32 GetPageCountInImageFile(IntPtr ImageWizHelper, string filename, string pwd); |
| 87 | + |
| 88 | + [DllImport("ImageWizHelper.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 89 | + private static extern Int32 getImageQuality(IntPtr ImageWizHelper, IntPtr objQuality); |
| 90 | + |
| 91 | + [DllImport("ImageWizHelper.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 92 | + private static extern Int32 CompressToTIFF(IntPtr ImageWizHelper, String[] InFileName, Int32 InFileCount, String OutputFileName, ResetOption Option); |
| 93 | + |
| 94 | + [DllImport("ImageWizHelper.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 95 | + private static extern Int32 CompressToPDF(IntPtr ImageWizHelper, String[] InFileName, Int32 InFileCount, String OutputFileName, ResetOption Option); |
| 96 | + |
| 97 | + [DllImport("ImageWizHelper.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 98 | + private static extern Int32 CompressToJpeg(IntPtr ImageWizHelper, String InFileName, String OutputFileName, ResetOption Option); |
| 99 | + |
| 100 | + [DllImport("ImageWizHelper.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 101 | + private static extern Int32 SetDPI(IntPtr ImageWizHelper, ImageDPI Dpi); |
| 102 | + |
| 103 | + [DllImport("ImageWizHelper.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 104 | + private static extern Int32 SetImageQuality(IntPtr ImageWizHelper, ImageQuality Quality); |
| 105 | + |
| 106 | + [DllImport("ImageWizHelper.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 107 | + private static extern void EnableExifRotation(IntPtr ImageWizHelper); |
| 108 | + |
| 109 | + [DllImport("ImageWizHelper.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 110 | + private static extern void DisableExifRotation(IntPtr ImageWizHelper); |
| 111 | + |
| 112 | + [DllImport("ImageWizHelper.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 113 | + private static extern Int32 GetErrorDescription(Int32 ErrorValue, StringBuilder ErrorDesc, Int32 MaxLen); |
| 114 | + |
| 115 | + [DllImport("ImageWizHelper.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 116 | + private static extern Int32 GetMaskImage(IntPtr ImageWizHelper, String FileName, String OutputFileName, Int32 PageNum, [MarshalAs(UnmanagedType.LPArray)] RECT[] RectData, Int32 RectCount, Int32 maskValue); |
| 117 | + |
| 118 | + [DllImport("ImageWizHelper.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 119 | + private static extern Int32 CompressPagesToTiff_Array(IntPtr ImageWizHelper, String InputFileName, String OutputFileName, Int32[] PageArray, Int32 PageArrayCount, Boolean Append, ResetOption Option); |
| 120 | + |
| 121 | + [DllImport("ImageWizHelper.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 122 | + private static extern Int32 CompressPagesToTiff_Range(IntPtr ImageWizHelper, String InputFileName, String OutputFileName, Int32 StartPageNum, Int32 EndPageNum, Boolean Append, ResetOption Option); |
| 123 | + |
| 124 | + [DllImport("ImageWizHelper.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 125 | + private static extern Int32 SetConversion(IntPtr ImageWizHelper, Int32 Conversion); |
| 126 | + |
| 127 | + [DllImport("ImageWizHelper.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 128 | + private static extern Int32 GetConversion(IntPtr ImageWizHelper, ref Int32 Conversion); |
| 129 | + |
| 130 | + [DllImport("ImageWizHelper.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] |
| 131 | + private static extern Int32 AppendToTiff(IntPtr ImageWizHelper, String InputFile, String OutPutFile, ResetOption Option); |
| 132 | + |
| 133 | + //Win API def |
| 134 | + [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)] |
| 135 | + private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]String lpFileName); |
| 136 | + |
| 137 | + public IntPtr ImageWizHandle { get; set; } |
| 138 | + String licPath = String.Empty; |
| 139 | + String[] lic_name = { "ImageWizHelper_extrieve_windows", "ImageWizHelper_extrieve_windows_with_data_pdf" };//license name |
| 140 | + |
| 141 | + public String DllName = "ImageWizHelper.dll"; |
| 142 | + public ImageWizHelper(String dll_path) |
| 143 | + { |
| 144 | + //Console.WriteLine(CurrentPath); |
| 145 | + String dllPath = Path.Combine(dll_path, DllName);//****Here We Are Taking The Dll Path From Where We Have Plased The Dll. Full Path With Name. |
| 146 | + String LogFileName = Path.Combine(dll_path, "ImageWizHelper_Error.log"); |
| 147 | + if (File.Exists(dllPath)) |
| 148 | + { |
| 149 | + //****Here We are Loading The Dll**** |
| 150 | + if(LoadLibrary(dllPath) == IntPtr.Zero) throw new Exception("Dll isn't Load Successfully|" + dll_path + "|" + DLL_NAME); |
| 151 | + StringBuilder error_info = new StringBuilder(1024); |
| 152 | + if (UnlockLibrary(dll_path, error_info) == 0)//unlock library using input. |
| 153 | + { |
| 154 | + if (error_info.Length > 0) |
| 155 | + throw new Exception(error_info.ToString()); |
| 156 | + } |
| 157 | + |
| 158 | + ImageWizHandle = Initialize(LogFileName); //Initilizing the Handle of ImageWizHelper |
| 159 | + if (ImageWizHandle == IntPtr.Zero) throw new Exception("unable to load memory"); |
| 160 | + } |
| 161 | + } |
| 162 | + public void UninitializeHelper() |
| 163 | + { |
| 164 | + if (ImageWizHandle != IntPtr.Zero) |
| 165 | + { |
| 166 | + Terminate(ImageWizHandle); |
| 167 | + ImageWizHandle = IntPtr.Zero; |
| 168 | + } |
| 169 | + } |
| 170 | + public Int32 GetPageCount(string filename, string pwd) |
| 171 | + { |
| 172 | + return GetPageCountInImageFile(ImageWizHandle, filename, pwd); |
| 173 | + } |
| 174 | + |
| 175 | + public Int32 getErrorDescription(Int32 error, StringBuilder error_buff, Int32 len) |
| 176 | + { |
| 177 | + return GetErrorDescription(error, error_buff, len); |
| 178 | + } |
| 179 | + |
| 180 | + //function for unlock the library using unlock functionality |
| 181 | + public Int32 UnlockLibrary(String lic_path, StringBuilder err_buff) |
| 182 | + { |
| 183 | + Int32 index = lic_path.IndexOf("bin"); |
| 184 | + String path = Path.Combine(lic_path.Substring(0, (lic_path.Length - lic_path.Substring(index).Length)), "DLL");//path of DLL |
| 185 | + if(!Directory.Exists(path)) |
| 186 | + { |
| 187 | + Directory.CreateDirectory(path);//if doesn't exist create directory of DLL |
| 188 | + } |
| 189 | + String licfile_path1 = Path.Combine(path, lic_name[0] + ".lic"); |
| 190 | + String licfile_path2 = licfile_path2 = Path.Combine(path, lic_name[1] + ".lic"); |
| 191 | + |
| 192 | + if (File.Exists(licfile_path1) && File.Exists(licfile_path2)) |
| 193 | + { |
| 194 | + throw new Exception("license should be only one"); |
| 195 | + } |
| 196 | + |
| 197 | + if (File.Exists(licfile_path1)) |
| 198 | + { |
| 199 | + return UnlockLibraryWithModuleIdentity(licfile_path1, err_buff); |
| 200 | + } |
| 201 | + else if (File.Exists(licfile_path2)) |
| 202 | + { |
| 203 | + UnlockLibraryWithModuleIdentity(licfile_path2, err_buff); |
| 204 | + } |
| 205 | + else |
| 206 | + { |
| 207 | + Console.WriteLine(path); |
| 208 | + throw new Exception("license doesn't exist in the DLL directory"); |
| 209 | + } |
| 210 | + return 0; |
| 211 | + } |
| 212 | + public Int32 CompressAndAddToTIFF(String[] InFileName, String OutputFileName, ResetOption Option) |
| 213 | + { |
| 214 | + return CompressToTIFF(ImageWizHandle, InFileName, InFileName.Length, OutputFileName, Option); |
| 215 | + } |
| 216 | + public int AppendToTIFF(String inputFile, String outputFile, ResetOption resetOption) |
| 217 | + { |
| 218 | + return AppendToTiff(ImageWizHandle, inputFile, outputFile, ResetOption.ResetAllDPI); |
| 219 | + } |
| 220 | + public Int32 CompressAndAddToPDF(String[] InFileName, String outputFile, ResetOption Option) |
| 221 | + { |
| 222 | + return CompressToPDF(ImageWizHandle, InFileName, InFileName.Length, outputFile, Option); |
| 223 | + } |
| 224 | + public Int32 CompressAndAddToJPG(String[] InFileName, String outputFile, ResetOption Option) |
| 225 | + { |
| 226 | + return CompressToPDF(ImageWizHandle, InFileName, InFileName.Length, outputFile, Option); |
| 227 | + } |
| 228 | + public Int32 GetMaskedImage(String inputFileName, String outputFile, Int32 PageNum, RECT[] RectData) |
| 229 | + { |
| 230 | + return GetMaskImage(ImageWizHandle, inputFileName, outputFile, PageNum, RectData, RectData.Length, 1); |
| 231 | + } |
| 232 | + |
| 233 | + public Int32 CompressToTiff_By_PageArray(String inputFileName, String outputFile, Int32[] PageArray, Boolean Append, ResetOption option) |
| 234 | + { |
| 235 | + return CompressPagesToTiff_Array(ImageWizHandle, inputFileName, outputFile, PageArray, PageArray.Length, Append, option); |
| 236 | + } |
| 237 | + |
| 238 | + public Int32 CompressToTiff_By_PageRange(String inputFileName, String outputFileName, Int32 startPage, Int32 EndPage, Boolean Append, ResetOption option = ResetOption.ResetAllDPI) |
| 239 | + { |
| 240 | + return CompressPagesToTiff_Range(ImageWizHandle, inputFileName, outputFileName, startPage, EndPage, Append, option); |
| 241 | + } |
| 242 | + |
| 243 | + public Int32 AppentTo_Tiff(String InputFile, String OutputFile, ResetOption option) |
| 244 | + { |
| 245 | + return AppendToTiff(ImageWizHandle, InputFile, OutputFile, option); |
| 246 | + } |
| 247 | + public Int32 SetConversion(ConversionType convertionType) |
| 248 | + { |
| 249 | + return SetConversion(ImageWizHandle, (Int32)convertionType); |
| 250 | + } |
| 251 | + public Int32 SetUpDPI(ImageDPI DPI) |
| 252 | + { |
| 253 | + return SetDPI(ImageWizHandle, DPI); |
| 254 | + } |
| 255 | + |
| 256 | + public Int32 SetupBPP(Int32 ConvType) |
| 257 | + { |
| 258 | + return SetConversion(ImageWizHandle, ConvType); |
| 259 | + } |
| 260 | + |
| 261 | + public ImageQuality GetImageQuality() |
| 262 | + { |
| 263 | + IntPtr objquality; |
| 264 | + objquality = new IntPtr(); |
| 265 | + |
| 266 | + int ret = getImageQuality(ImageWizHandle, objquality); |
| 267 | + |
| 268 | + if (ret != 0) { return ImageQuality.unknown; } |
| 269 | + |
| 270 | + return ImageQuality.Photo_Quality; |
| 271 | + |
| 272 | + } |
| 273 | + public Int32 GetConversionType() |
| 274 | + { |
| 275 | + Int32 ConvType = 0; |
| 276 | + GetConversion(ImageWizHandle, ref ConvType); |
| 277 | + return ConvType; |
| 278 | + } |
| 279 | + |
| 280 | + public Int32 TerminateHandle(IntPtr Handle) |
| 281 | + { |
| 282 | + return Terminate(Handle); |
| 283 | + } |
| 284 | + } |
| 285 | +} |
0 commit comments