private void generateResourceMenuItem_Click(object sender, EventArgs e) { Point origStart = GetOriginalXY(selStartMousePos); Point origEnd = GetOriginalXY(selEndMousePos); if (selStartMousePos.X != 0 || selStartMousePos.Y != 0) { int top = origStart.Y < origEnd.Y ? origStart.Y : origEnd.Y; int left = origStart.X < origEnd.X ? origStart.X : origEnd.X; int width = Math.Abs(origEnd.X - origStart.X); int height = Math.Abs(origEnd.Y - origStart.Y); Rectangle box = new Rectangle(left, top, width, height); Bitmap bitmap = ((Bitmap)image).Clone(box, ((Bitmap)image).PixelFormat); if (Statics.DTE.Solution.IsOpen) { foreach (EnvDTE.Project proj in Statics.DTE.Solution.Projects) { if (proj.Name.Contains("Test")) { try { ImageInputForm inputForm = new ImageInputForm(); inputForm.ShowDialog(); Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider(); string resFile = Path.GetDirectoryName(proj.FileName) + @"\Properties\Resources.resx"; string resDesginFile = Path.GetDirectoryName(proj.FileName) + @"\Properties\Resources.Designer.cs"; System.Resources.ResXResourceReader reader = new System.Resources.ResXResourceReader(resFile); using (System.Resources.ResXResourceWriter writer = new System.Resources.ResXResourceWriter(resFile + ".new")) { System.Collections.IDictionaryEnumerator iterator = reader.GetEnumerator(); while (iterator.MoveNext()) { writer.AddResource(iterator.Key.ToString(), iterator.Value); } writer.AddResource(inputForm.Input, bitmap); writer.Generate(); } File.Copy(resFile + ".new", resFile, true); File.Delete(resFile + ".new"); string[] unMatched; System.CodeDom.CodeCompileUnit unit = System.Resources.Tools.StronglyTypedResourceBuilder.Create(resFile, "Resources", proj.Properties.Item("DefaultNamespace").Value + ".Properties", codeProvider, true, out unMatched); using (StreamWriter designWriter = new StreamWriter(resDesginFile)) { codeProvider.GenerateCodeFromCompileUnit(unit, designWriter, new System.CodeDom.Compiler.CodeGeneratorOptions()); } MessageBox.Show("Image generation succeeded", "Resources Updated"); return; } catch (Exception ex) { MessageBox.Show("Image generation failed\n" + ex.Message, "Resources Did Not Update"); } } } MessageBox.Show("You need to have a project open, named *Test*", "Resources Did Not Update"); return; } MessageBox.Show("You need to have a solution open with a project named *Test*", "Resources Did Not Update"); return; } }
private void CreateResourceFromImage(bool entire) { Bitmap bitmap = null; if (entire) { bitmap = (Bitmap)(zoomPanControl.ZoomPanImage.Clone()); } else if (HasImageSelection && !entire) { bitmap = ((Bitmap)zoomPanControl.ZoomPanImage).Clone(partBox, ((Bitmap)zoomPanControl.ZoomPanImage).PixelFormat); } if (bitmap != null) { if (Statics.DTE.Solution.IsOpen) { foreach (EnvDTE.Project proj in Statics.DTE.Solution.Projects) { if (proj.Name.Contains("Test")) { try { string resFile, resDesignFile, resNameSpace; System.CodeDom.Compiler.CodeDomProvider provider; if (proj.FileName.EndsWith("vbproj")) { resFile = Path.GetDirectoryName(proj.FileName) + @"\My Project\Resources.resx"; resDesignFile = Path.GetDirectoryName(proj.FileName) + @"\My Project\Resources.Designer.vb"; vbNS = resNameSpace = proj.Properties.Item("RootNamespace").Value.ToString(); provider = new Microsoft.VisualBasic.VBCodeProvider(); } else { resFile = Path.GetDirectoryName(proj.FileName) + @"\Properties\Resources.resx"; resDesignFile = Path.GetDirectoryName(proj.FileName) + @"\Properties\Resources.Designer.cs"; resNameSpace = proj.Properties.Item("DefaultNamespace").Value + ".Properties"; provider = new Microsoft.CSharp.CSharpCodeProvider(); } ImageInputForm inputForm = new ImageInputForm(); inputForm.ShowDialog(); System.Resources.ResXResourceReader reader = new System.Resources.ResXResourceReader(resFile); using (System.Resources.ResXResourceWriter writer = new System.Resources.ResXResourceWriter(resFile + ".new")) { System.Collections.IDictionaryEnumerator iterator = reader.GetEnumerator(); while (iterator.MoveNext()) { writer.AddResource(iterator.Key.ToString(), iterator.Value); } writer.AddResource(inputForm.Input, bitmap); writer.Generate(); } File.Copy(resFile + ".new", resFile, true); File.Delete(resFile + ".new"); string[] unMatched; System.CodeDom.CodeCompileUnit unit = System.Resources.Tools.StronglyTypedResourceBuilder.Create(resFile, "Resources", resNameSpace, provider, true, out unMatched); using (StreamWriter designWriter = new StreamWriter(resDesignFile)) { provider.GenerateCodeFromCompileUnit(unit, designWriter, new System.CodeDom.Compiler.CodeGeneratorOptions()); } MessageBox.Show("Image generation succeeded", "Resources Updated"); if (entire) { entireResName = inputForm.Input; NotifyPropertyChanged("EntireResName"); } else { partResName = inputForm.Input; NotifyPropertyChanged("PartialResName"); } return; } catch (Exception ex) { MessageBox.Show("Image generation failed\n" + ex.Message, "Resources Did Not Update"); return; } } } MessageBox.Show("You need to have a project open, named *Test*", "Resources Did Not Update"); return; } MessageBox.Show("You need to have a solution open with a project named *Test*", "Resources Did Not Update"); return; } }