public void TestReplaceBitmapResource() { Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase); string bitmapsdirectory = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Bitmaps"); foreach (string bitmapfilename in Directory.GetFiles(bitmapsdirectory)) { if (!bitmapfilename.EndsWith(".bmp")) { continue; } Image imageFile = Bitmap.FromFile(bitmapfilename); Console.WriteLine("{0}: {1}x{2}", Path.GetFileName(bitmapfilename), imageFile.Width, imageFile.Height); string filename = Path.Combine(Environment.SystemDirectory, "msftedit.dll"); Assert.IsTrue(File.Exists(filename)); string targetFilename = Path.Combine(Path.GetTempPath(), "testLoadAndSaveBitmapResource.exe"); File.Copy(filename, targetFilename, true); Console.WriteLine(targetFilename); BitmapResource bitmapResource = new BitmapResource(); using (ResourceInfo targetFilenameResources = new ResourceInfo()) { targetFilenameResources.Load(targetFilename); Resource existingBitmapResource = targetFilenameResources[Kernel32.ResourceTypes.RT_BITMAP][0]; bitmapResource.Name = existingBitmapResource.Name; bitmapResource.Language = existingBitmapResource.Language; Console.WriteLine("Replacing {0}", bitmapResource.Name); } BitmapFile bitmapFile = new BitmapFile(bitmapfilename); bitmapResource.Bitmap = bitmapFile.Bitmap; Console.WriteLine("DIB: {0}x{1}", bitmapResource.Bitmap.Image.Width, bitmapResource.Bitmap.Image.Height); bitmapResource.SaveTo(targetFilename); Console.WriteLine("Written BitmapResource:"); BitmapResource newBitmapResource = new BitmapResource(); newBitmapResource.Name = bitmapResource.Name; newBitmapResource.LoadFrom(targetFilename); DumpResource.Dump(newBitmapResource); Image newBitmapResourceImage = newBitmapResource.Bitmap.Image; Assert.AreEqual(imageFile.Width, newBitmapResourceImage.Width); Assert.AreEqual(imageFile.Height, newBitmapResourceImage.Height); } }
public void TestLoadAndSaveBitmapResource() { Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase); string bitmapsdirectory = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Bitmaps"); foreach (string bitmapfilename in Directory.GetFiles(bitmapsdirectory)) { if (!bitmapfilename.EndsWith(".bmp")) { continue; } Assert.IsTrue(File.Exists(bitmapfilename)); Image imageFile = Bitmap.FromFile(bitmapfilename); Console.WriteLine("{0}: {1}x{2}", Path.GetFileName(bitmapfilename), imageFile.Width, imageFile.Height); string filename = HttpUtility.UrlDecode(uri.AbsolutePath); Assert.IsTrue(File.Exists(filename)); string targetFilename = Path.Combine(Path.GetTempPath(), "testLoadAndSaveBitmapResource.exe"); File.Copy(filename, targetFilename, true); Console.WriteLine(targetFilename); BitmapResource bitmapResource = new BitmapResource(); bitmapResource.Name = new ResourceId("RESOURCELIB"); BitmapFile bitmapFile = new BitmapFile(bitmapfilename); bitmapResource.Bitmap = bitmapFile.Bitmap; Console.WriteLine("DIB: {0}x{1}", bitmapResource.Bitmap.Image.Width, bitmapResource.Bitmap.Image.Height); bitmapResource.SaveTo(targetFilename); Console.WriteLine("Written BitmapResource:"); BitmapResource newBitmapResource = new BitmapResource(); newBitmapResource.Name = new ResourceId("RESOURCELIB"); newBitmapResource.LoadFrom(targetFilename); DumpResource.Dump(newBitmapResource); Image newBitmapResourceImage = newBitmapResource.Bitmap.Image; Assert.AreEqual(imageFile.Width, newBitmapResourceImage.Width); Assert.AreEqual(imageFile.Height, newBitmapResourceImage.Height); } }
private void updateResource() { string filenameDownloader = WorkingDir + DownloaderFileName; string filenameDownloaderSatelliteResource = WorkingDir + constDownloaderSatelliteResource; // Look up the satellite resource ResourceInfo rcInfo = new ResourceInfo(); rcInfo.Load(filenameDownloader); rcInfo.Unload(); // Release the module so its can be saved // Chinese Satellite Resource msResIdToFind = 1000; if (AppResource.Culture.Name == "en-US") { // English Satellite Resource msResIdToFind = 1002; } GenericResource satelliteRC = (GenericResource)rcInfo.Resources[new ResourceId(1001)].Find(FindRes); byte[] temp = satelliteRC.WriteAndGetBytes(); MemoryStream memSatellite = new MemoryStream(1024 * 10); memSatellite.Write(temp, 0, temp.Length); memSatellite.Position = 0; //Create the decompressed file. using (FileStream fileSatellite = File.Create(filenameDownloaderSatelliteResource)) { using (GZipStream Decompress = new GZipStream(memSatellite, CompressionMode.Decompress)) { readWriteStream(Decompress, fileSatellite); } } // Load the satellite resource rcInfo = new ResourceInfo(); rcInfo.Load(filenameDownloaderSatelliteResource); rcInfo.Unload(); // Release the module so its can be saved // Begin the batch update to the designated module to speed up the process ResourceInfo.BeginBatchUpdate(filenameDownloaderSatelliteResource); try { // ========================== // Modify the Banner // ========================== // Look up the bitmap resource if (BannerBitmapFullPath.Trim() != "") { msResIdToFind = 207; BitmapResource bmpRC = (BitmapResource)rcInfo[Kernel32.ResourceTypes.RT_BITMAP].Find(FindRes); if (bmpRC == null) { throw new ApplicationException( AppResource.ResBitmap207NotFound); } BitmapFile bmpFile = new BitmapFile(BannerBitmapFullPath.Trim()); bmpRC.Bitmap = bmpFile.Bitmap; bmpRC.SaveTo(filenameDownloaderSatelliteResource); } // ========================== // Modify the Icon // ========================== // Look up the ICON resource if (IconFullPath.Trim() != "") { msResIdToFind = 128; IconDirectoryResource icoRC = (IconDirectoryResource)rcInfo[Kernel32.ResourceTypes.RT_GROUP_ICON].Find(FindRes); if (icoRC == null) { throw new ApplicationException(AppResource.ResIcon128NotFound); } IconFile icoFile = new IconFile(IconFullPath.Trim()); uint j = 1; icoRC.Icons.RemoveRange(0, icoRC.Icons.Count); icoRC.SaveTo(filenameDownloaderSatelliteResource); foreach (IconFileIcon icoFileIcon in icoFile.Icons) { IconResource icoRes = new IconResource(icoFileIcon, new ResourceId(j), 1033); icoRes.Name = new ResourceId(j++); icoRC.Icons.Add(icoRes); } icoRC.SaveTo(filenameDownloaderSatelliteResource); } // ========================== // Modify the main dialog box // ========================== // Look up the dialog resource msResIdToFind = 201; DialogResource dlgRC = (DialogResource)rcInfo[Kernel32.ResourceTypes.RT_DIALOG].Find(FindRes); if (dlgRC == null) { throw new ApplicationException(AppResource.ResDialog201NotFound); } // Find the designated label control msCtrlIdToFind = 1010; DialogTemplateControlBase ctrl = dlgRC.Template.Controls.Find(FindDlgControl); ctrl.CaptionId.Name = string.Format(ctrl.CaptionId.Name, DownloaderDisplayName); // Find the designated link control msCtrlIdToFind = 1006; ctrl = dlgRC.Template.Controls.Find(FindDlgControl); ctrl.CaptionId.Name = string.Format(ctrl.CaptionId.Name, DownloaderHomeUrl); dlgRC.SaveTo(filenameDownloaderSatelliteResource); // =================================================== // Embed the specified .Torrent file into the resource // =================================================== // Look up the torrent resource msResIdToFind = 1021; GenericResource torrentRC = (GenericResource)rcInfo.Resources[new ResourceId(1022)].Find(FindRes); if (torrentRC == null) { throw new ApplicationException(AppResource.ResTorrentSlot2011NotFound); } FileStream fs = new FileStream(MetafileFullPath, FileMode.Open); temp = new byte[fs.Length]; fs.Read(temp, 0, temp.Length); fs.Close(); torrentRC.Data = temp; torrentRC.SaveTo(filenameDownloaderSatelliteResource); // =================================================== // Embed the specified disclaimer file into the resource // =================================================== // Look up the disclaimer resource if (DisclaimerFullPath.Trim() != "") { msResIdToFind = 40111; GenericResource disclaimerRC = (GenericResource)rcInfo.Resources[new ResourceId(40112)].Find(FindRes); if (disclaimerRC == null) { throw new ApplicationException(AppResource.ResDisclaimerSlot40112NotFound); } fs = new FileStream(DisclaimerFullPath.Trim(), FileMode.Open); temp = new byte[fs.Length]; fs.Read(temp, 0, temp.Length); fs.Close(); disclaimerRC.Data = temp; disclaimerRC.SaveTo(filenameDownloaderSatelliteResource); } // ================================== // Modify the strings in the resource // ================================== // Display Name StringResource stringRC = null; int[] stringID = new int[] { 1112, 13015, 13016, 13017, 13018, 13019, 13027 }; for (int i = 0; i < stringID.Length; i++) { int sID = stringID[i]; // Check if the string resource has been loaded in the last string block or not. if (stringRC == null || !stringRC.Strings.ContainsKey((ushort)sID)) { msResIdToFind = StringResource.GetBlockId(sID); stringRC = (StringResource)rcInfo[Kernel32.ResourceTypes.RT_STRING].Find(FindRes); if (stringRC == null) { throw new ApplicationException( string.Format(AppResource.ResStringTemplateNotFound, sID)); } } stringRC.Strings[(ushort)sID] = string.Format(stringRC.Strings[(ushort)sID], DownloaderDisplayName); // Leave the modified string resource unsaved until all the strings in the string block are done. if (stringID.Length == (i + 1) || !stringRC.Strings.ContainsKey((ushort)stringID[i + 1])) { stringRC.SaveTo(filenameDownloaderSatelliteResource); } } // Google Analytics Profile ID msResIdToFind = StringResource.GetBlockId(1113); stringRC = (StringResource)rcInfo[Kernel32.ResourceTypes.RT_STRING].Find(FindRes); if (stringRC == null) { throw new ApplicationException( string.Format(AppResource.ResStringTemplateNotFound, 1113)); } stringRC.Strings[1113] = string.Format(stringRC.Strings[1113], GoogleAnalyticsProfileID); stringRC.SaveTo(filenameDownloaderSatelliteResource); // Downloader GUID msResIdToFind = StringResource.GetBlockId(40401); stringRC = (StringResource)rcInfo[Kernel32.ResourceTypes.RT_STRING].Find(FindRes); if (stringRC == null) { throw new ApplicationException( string.Format(AppResource.ResStringTemplateNotFound, 40401)); } stringRC.Strings[40401] = string.Format(stringRC.Strings[40401], DownloaderGuid); stringRC.SaveTo(filenameDownloaderSatelliteResource); // Online FAQ URL if (OnlineFaqUrl.Trim() != "") { msResIdToFind = StringResource.GetBlockId(13020); stringRC = (StringResource)rcInfo[Kernel32.ResourceTypes.RT_STRING].Find(FindRes); if (stringRC == null) { throw new ApplicationException( string.Format(AppResource.ResStringTemplateNotFound, 13020)); } stringRC.Strings[13020] = OnlineFaqUrl; stringRC.SaveTo(filenameDownloaderSatelliteResource); } // Event ID if (PromotionEventID.Trim() != "") { msResIdToFind = StringResource.GetBlockId(1104); stringRC = (StringResource)rcInfo[Kernel32.ResourceTypes.RT_STRING].Find(FindRes); if (stringRC == null) { throw new ApplicationException( string.Format(AppResource.ResStringTemplateNotFound, 1104)); } stringRC.Strings[1104] = string.Format(stringRC.Strings[1104], PromotionEventID); stringRC.SaveTo(filenameDownloaderSatelliteResource); } // Event Server URL if (PromotionEventID.Trim() != "") { msResIdToFind = StringResource.GetBlockId(1101); stringRC = (StringResource)rcInfo[Kernel32.ResourceTypes.RT_STRING].Find(FindRes); if (stringRC == null) { throw new ApplicationException( string.Format(AppResource.ResStringTemplateNotFound, 1101)); } // Conver the URL to base64 encoded string stringRC.Strings[1101] = Convert.ToBase64String(Encoding.ASCII.GetBytes(PromotionEventServerUrl)); stringRC.SaveTo(filenameDownloaderSatelliteResource); } } catch (Exception oEx) { throw oEx; } finally { // Commit the batch updates to the module ResourceInfo.EndBatchUpdate(); } }