public void CreateIcon(string path, System.Drawing.Icon icon) { using (var file = new FileStream(path, FileMode.Create, FileAccess.Write)) { icon.Save(file); } }
/// <summary> /// 图标文件转为流 /// </summary> /// <param name="imageIn"></param> /// <returns></returns> public static byte[] ImageToByteArray(System.Drawing.Icon imageIn) { MemoryStream ms = new MemoryStream(); imageIn.Save(ms); return(ms.ToArray()); }
// http://stackoverflow.com/questions/220465/using-256-x-256-vista-icon-in-application/1945764#1945764 // Based on: http://www.codeproject.com/KB/cs/IconExtractor.aspx // And a hint from: http://www.codeproject.com/KB/cs/IconLib.aspx public static System.Drawing.Bitmap ExtractVistaIcon(System.Drawing.Icon icoIcon) { System.Drawing.Bitmap bmpPngExtracted = null; try { byte[] srcBuf = null; using (MemoryStream stream = new MemoryStream()) { icoIcon.Save(stream); srcBuf = stream.ToArray(); } const int SizeICONDIR = 6; const int SizeICONDIRENTRY = 16; int iCount = BitConverter.ToInt16(srcBuf, 4); for (int iIndex = 0; iIndex < iCount; iIndex++) { int iWidth = srcBuf[SizeICONDIR + SizeICONDIRENTRY * iIndex]; int iHeight = srcBuf[SizeICONDIR + SizeICONDIRENTRY * iIndex + 1]; int iBitCount = BitConverter.ToInt16(srcBuf, SizeICONDIR + SizeICONDIRENTRY * iIndex + 6); if (iWidth == 0 && iHeight == 0 && iBitCount == 32) { int iImageSize = BitConverter.ToInt32(srcBuf, SizeICONDIR + SizeICONDIRENTRY * iIndex + 8); int iImageOffset = BitConverter.ToInt32(srcBuf, SizeICONDIR + SizeICONDIRENTRY * iIndex + 12); MemoryStream destStream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(destStream); writer.Write(srcBuf, iImageOffset, iImageSize); destStream.Seek(0, SeekOrigin.Begin); bmpPngExtracted = new System.Drawing.Bitmap(destStream); // This is PNG! :) break; } } } catch { return(null); } return(bmpPngExtracted); }
/// <summary> /// This method converts <see cref="System.Drawing.Icon"/> type to <see cref="BitmapFrame"/> /// </summary> /// <param name="icon"></param> /// <returns></returns> public static BitmapFrame IconToBitmapFrame(System.Drawing.Icon icon) { MemoryStream ms = new MemoryStream(); BitmapFrame result; try { icon.Save(ms); result = BitmapFrame.Create(ms); } catch (Exception e) { MessageBox.Show ( e.Message, null, MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly ); result = null; } return(result); }
public static byte[] IconToBytes(System.Drawing.Icon icon) { using (MemoryStream ms = new MemoryStream()) { icon.Save(ms); return(ms.ToArray()); } }
/// <summary> /// Extracts the icon using System.Drawing.Icon /// </summary> /// <param name="file"></param> /// <remarks> /// Reference: /// http://msdn.microsoft.com/en-us/library/ms404308.aspx /// </remarks> public static void ExtractUsingIcon(string file) { System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(file); using (FileStream stream = File.OpenWrite(Path.GetFileName(file) + ".ico")) { icon.Save(stream); } }
public BitmapFrame IconToImageSource(System.Drawing.Icon ico) { MemoryStream iconStream = new MemoryStream(); ico.Save(iconStream); iconStream.Seek(0, SeekOrigin.Begin); return(BitmapFrame.Create(iconStream)); }
public static Icon IconToEto(System.Drawing.Icon icon) { using (var ms = new MemoryStream()) { icon.Save(ms); return(new Icon(ms)); } }
void SendFavIcon() { System.Drawing.Icon Icon = (System.Drawing.Icon)global::AdvancedFTPServer.Properties.Resources.ResourceManager.GetObject("favicon"); MemoryStream str = new MemoryStream(); Icon.Save(str); SendHeader((int)str.Length, "200 OK", "image/x-icon"); SendData(str.GetBuffer()); }
public static BitmapSource IconToBitmapSource(System.Drawing.Icon icon) { using (var stream = new System.IO.MemoryStream()) { icon.Save(stream); stream.Seek(0, System.IO.SeekOrigin.Begin); return(BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad)); } }
/// <summary>Creates a new instance of the form. Should call setSpiraProjects() and setSoltion() after calling this.</summary> internal wpfAssignProject() { try { InitializeComponent(); //Get the resources. this._resources = Connect.getCultureResource; //Title this.Title = this._resources.GetString("strAssignProjectTitle"); //Icon try { System.Drawing.Icon ico = (System.Drawing.Icon) this._resources.GetObject("icoLogo"); MemoryStream icoStr = new MemoryStream(); ico.Save(icoStr); icoStr.Seek(0, SeekOrigin.Begin); this.Icon = BitmapFrame.Create(icoStr); } catch (Exception ex) { Connect.logEventMessage("wpfNewSpiraProject::.ctor::MakeIcon", ex, System.Diagnostics.EventLogEntryType.Error); } //Load logos and images. this.imgLogo.Source = getImage("imgLogo", new Size()).Source; this.imgLogo.Height = imgLogo.Source.Height; this.imgLogo.Width = imgLogo.Source.Width; this.btnNew.Content = getImage("imgAdd", new Size(16, 16)); this.btnEdit.Content = getImage("imgEdit", new Size(16, 16)); this.btnDelete.Content = getImage("imgDelete", new Size(16, 16)); //Set events. this.btnEdit.IsEnabledChanged += new DependencyPropertyChangedEventHandler(btn_IsEnabledChanged); this.btnDelete.IsEnabledChanged += new DependencyPropertyChangedEventHandler(btn_IsEnabledChanged); this.btnNew.Click += new RoutedEventHandler(btnNewEdit_Click); this.btnEdit.Click += new RoutedEventHandler(btnNewEdit_Click); this.btnAdd.Click += new RoutedEventHandler(btnAdd_Click); this.btnRemove.Click += new RoutedEventHandler(btnRemove_Click); this.btnSave.Click += new RoutedEventHandler(btnSave_Click); this.btnCancel.Click += new RoutedEventHandler(btnCancel_Click); this.btnDelete.Click += new RoutedEventHandler(btnDelete_Click); this.btn_IsEnabledChanged(this.btnEdit, new DependencyPropertyChangedEventArgs()); this.btn_IsEnabledChanged(this.btnDelete, new DependencyPropertyChangedEventArgs()); //Set the caption. this.setRTFCaption(); } catch (Exception ex) { Connect.logEventMessage("wpfAssignProject::.ctor", ex, System.Diagnostics.EventLogEntryType.Error); } }
} // End Sub ReadUsingReportViewer public static void ExtractXMLQuireIcon() { System.Drawing.Icon ico = System.Drawing.Icon.ExtractAssociatedIcon(@"D:\Stefan.Steiger\Desktop\Quire\XMLQuire.exe"); using (System.IO.Stream strm = new System.IO.FileStream(@"D:\Stefan.Steiger\Desktop\Quire\myicon.ico", System.IO.FileMode.OpenOrCreate)) { ico.Save(strm); strm.Flush(); strm.Close(); } // End Using strm } // End Sub ExtractXMLQuireIcon
private static string EncodedImage(string filePath, string extension) { string encodedImage; if (extension.ToUpper() == ".ICO") { System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(filePath); using (MemoryStream ms = new MemoryStream()) { if (icon != null) { icon.Save(ms); } byte[] imageBytes = ms.ToArray(); encodedImage = Convert.ToBase64String(imageBytes); } return(encodedImage); } System.Drawing.Image image = System.Drawing.Image.FromFile(filePath, true); ImageFormat format = null; switch (extension.ToUpper()) { case ".GIF": format = ImageFormat.Gif; break; case ".JPG": format = ImageFormat.Jpeg; break; case ".PNG": format = ImageFormat.Png; break; } if (format == null) { return(null); } using (MemoryStream ms = new MemoryStream()) { image.Save(ms, format); byte[] imageBytes = ms.ToArray(); encodedImage = Convert.ToBase64String(imageBytes); } return(encodedImage); }
/// <summary> /// Extracts the icon using Win32 API. /// </summary> /// <param name="strFile"></param> /// <remarks> /// References: /// http://support.microsoft.com/kb/319350 /// http://support.microsoft.com/kb/319340 /// http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/60610aff-2dfd-4d52-9c4d-638d514100d0 /// </remarks> public static void ExtractUsingWinAPI(string strFile) { Win32.SHFILEINFO shinfo = new Win32.SHFILEINFO(); IntPtr hImgSmall = Win32.SHGetFileInfo(strFile, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON); //The icon is returned in the hIcon member of the shinfo struct System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon); using (FileStream stream = File.OpenWrite(Path.GetFileName(strFile) + ".ico")) { icon.Save(stream); } }
protected virtual HttpResponseMessage Recurse(HttpListenerRequest req, string[] uri) { Master.LineLog.Debug("recursing " + uri.Length + " " + uri.FirstOrDefault()); if (uri.Length == 0) { return(null); } var types = this.GetType().GetNestedTypes(); var atypes = types.Where(t => (t.GetCustomAttribute(typeof(Master.PageAddressRenderAttribute)) is Master.PageAddressRenderAttribute)); var type = atypes.FirstOrDefault(t => (t.GetCustomAttribute(typeof(Master.PageAddressRenderAttribute)) as Master.PageAddressRenderAttribute).PageAddress == uri.First()); if (type != null) { var tt = type.GetConstructor(new Type[] { }).Invoke(new object[] { }); return((HttpResponseMessage)type.GetMethod("Render").Invoke(tt, new object[] { req, uri.Skip(1).ToArray() })); } if (req.RawUrl == "/favicon.ico") { System.Drawing.Icon obj = (System.Drawing.Icon)App.ResourceManager.GetObject("Icon" /*, App.Culture*/); byte[] res; using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { obj.Save(stream); res = stream.ToArray(); stream.Close(); } return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(res) }); } if (!uri.FirstOrDefault().Contains("/") && !uri.FirstOrDefault().Contains("..")) { if (System.IO.File.Exists("InternalServer/Swagger/" + uri.FirstOrDefault())) { return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(System.IO.File.ReadAllText("InternalServer/Swagger/" + uri.FirstOrDefault())) }); } if (System.IO.File.Exists("InternalServer/Themes/" + uri.FirstOrDefault())) { return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(System.IO.File.ReadAllText("InternalServer/Themes/" + uri.FirstOrDefault()) /*, Encoding.UTF8, "text/css"*/) }); } } return(new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent("<html><body>404: " + req.RawUrl + " not found. <a href='" + (req.UrlReferrer?.ToString() ?? "/") + "'>Return.</a></body></html>") }); }
private void buttonSaveIco_Click(object sender, EventArgs e) { using (var saveFileDialog = new SaveFileDialog { Filter = "Windows Icon files|*.ico|All files|*.*" }) { if (saveFileDialog.ShowDialog(this) == DialogResult.OK) { using (var stream = File.Create(saveFileDialog.FileName)) _icon.Save(stream); } } }
public static BitmapImage FromDrawingImage(System.Drawing.Icon ico) { var ms = new MemoryStream(); ico.Save(ms); var bImg = new BitmapImage(); bImg.BeginInit(); bImg.StreamSource = new MemoryStream(ms.ToArray()); bImg.EndInit(); return(bImg); }
// 아이콘을 아이콘 리소스로 변경 private static BitmapImage ToImageSource(System.Drawing.Icon icon, string imgtype) { var memoryStream = new MemoryStream(); icon.Save(memoryStream); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memoryStream; bitmapImage.EndInit(); return(bitmapImage); }
/// <summary> /// Returns the source of an ICON object (for use in WPF elements) /// </summary> /// <param name="ico"></param> /// <returns></returns> public static ImageSource GetImageSrc(this System.Drawing.Icon ico) { using (var ms = new MemoryStream()) { ico.Save(ms); ms.Position = 0; var bi = new BitmapImage(); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; bi.StreamSource = ms; bi.EndInit(); return(bi); } }
public static string EncodeIco(string filePath) { string encodedImage; System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(filePath); using (MemoryStream ms = new MemoryStream()) { icon?.Save(ms); byte[] imageBytes = ms.ToArray(); encodedImage = Convert.ToBase64String(imageBytes); } return(encodedImage); }
static void SaveBmpImageAsIcon(string imagePath, byte[] imageBytes, System.Drawing.Image image) { using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap)) graphics.DrawImage(image, 0, 0, image.Width, image.Height); IntPtr iconHandle = bitmap.GetHicon(); try { using (System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(iconHandle)) { WriteIconToFile(imagePath, f => icon.Save(f)); } } finally { DestroyIcon(iconHandle); } } }
public static ImageSource GetIcon(string s) { System.Drawing.Icon icon = ManagedWinapi.ExtendedFileInfo.GetIconForFilename(s, false); if (icon == null) { return(null); } using (MemoryStream ms = new MemoryStream()) { icon.Save(ms); var dec = new IconBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); return(dec.Frames[0]); } }
public static void CreateIcon(string Name) { using (Stream stream = Application.GetResourceStream(new Uri("pack://*****:*****@"\Image\" + Name + ".ico")) { if (!Directory.Exists(Path + @"\Image\")) { Directory.CreateDirectory(Path + @"\Image\"); } using (Stream stream1 = new StreamWriter(Path + @"\Image\" + Name + ".ico", true).BaseStream) myIcon.Save(stream1); } } }
public static System.Windows.Media.Imaging.BitmapFrame GetIcon(string filename) { string ext = Path.GetExtension(filename); if (cache.ContainsKey(ext)) { return(cache[ext]); } else { System.Drawing.Icon i = IconReader.GetFileIcon(filename, IconReader.IconSize.Large, false); MemoryStream iconStream = new MemoryStream(); i.Save(iconStream); iconStream.Seek(0, SeekOrigin.Begin); var bf = System.Windows.Media.Imaging.BitmapFrame.Create(iconStream); bf = ResizeHelper(bf, 16, 16, BitmapScalingMode.Fant); cache.Add(ext, bf); return(bf); } }
public wpfNewSpiraProject() { try { InitializeComponent(); //Get resources. this._resources = Connect.getCultureResource; //Set the title & icon. this.Title = this._resources.GetString("strNewSpiraProject"); try { System.Drawing.Icon ico = (System.Drawing.Icon) this._resources.GetObject("icoLogo"); MemoryStream icoStr = new MemoryStream(); ico.Save(icoStr); icoStr.Seek(0, SeekOrigin.Begin); this.Icon = BitmapFrame.Create(icoStr); } catch (Exception ex) { Connect.logEventMessage("wpfNewSpiraProject::.ctor::MakeIcon", ex, System.Diagnostics.EventLogEntryType.Error); } //Set initial colors and form status. this.barProg.Foreground = (Brush) new System.Windows.Media.BrushConverter().ConvertFrom(this._resources.GetString("barForeColor")); this.barProg.IsIndeterminate = false; this.barProg.Value = 0; this.grdAvailProjs.IsEnabled = false; this.grdEntry.IsEnabled = true; this.btnConnect.Tag = false; this.btnConnect.Click += new RoutedEventHandler(btnConnect_Click); int num = this.cmbProjectList.Items.Add("-- No Projects Available --"); this.cmbProjectList.SelectedIndex = num; this.cmbProjectList.SelectionChanged += new SelectionChangedEventHandler(cmbProjectList_SelectionChanged); } catch (Exception ex) { Connect.logEventMessage("wpfNewSpiraProject::.ctor", ex, System.Diagnostics.EventLogEntryType.Error); } }
// 프로그램 시작 시 UI 변경하기 private void StartUI() { // 버튼 텍스트 변경 this.StartButton.Content = "중지"; // 버튼 비활성화 this.SetTime.IsEnabled = false; this.RepeatUpButton.IsEnabled = false; this.RepeatDownButton.IsEnabled = false; this.DirectoryButton.IsEnabled = false; this.EndButton.IsEnabled = false; this.Directory.IsEnabled = false; // 프로그레스바 불투명하게 변경 this.Progress.Opacity = 1; // 프로그레스바 값 불투명하게 변경 this.ProgressValue.Opacity = 1; // 아이콘 변경 System.Drawing.Icon Icon = PerfMon.Properties.Resources.Sign_Record_Icon; MemoryStream iconStream = new MemoryStream(); Icon.Save(iconStream); iconStream.Seek(0, SeekOrigin.Begin); BitmapFrame NewIcon = BitmapFrame.Create(iconStream); this.Icon = NewIcon; // 이미지 변경 System.Drawing.Bitmap Image = PerfMon.Properties.Resources.Sign_Record_Image; MemoryStream imgStream = new MemoryStream(); Image.Save(imgStream, System.Drawing.Imaging.ImageFormat.Bmp); imgStream.Seek(0, SeekOrigin.Begin); BitmapFrame NewImage = BitmapFrame.Create(imgStream); MainImage.Source = NewImage; // 타이머 초기화 AddTimeSpan = TimeSpan.Zero; }
static void Main(string[] args) { System.Collections.Generic.List <FSI> ls = new System.Collections.Generic.List <FSI>(); string path = @"C:\Program Files\dotnet"; // string[] entries = System.IO.Directory.GetFileSystemEntries(path); System.IO.FileSystemInfo[] entries = new System.IO.DirectoryInfo(path).GetFileSystemInfos("*.*", System.IO.SearchOption.AllDirectories); foreach (System.IO.FileSystemInfo fsi in entries) { if ((fsi.Attributes & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory) { ls.Add(new FSI(fsi.FullName, true)); continue; } System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(fsi.FullName); //using (System.IO.FileStream fs = System.IO.File.OpenWrite("")) //{ // icon.Save(fs); //} using (System.IO.Stream fs = new System.IO.MemoryStream()) { icon.Save(fs); fs.Position = 0; byte[] iconBytes = Ico2Png.GetIcon(fs); // System.IO.File.WriteAllBytes(@"d:\test.png", iconBytes); ls.Add(new FSI(fsi.FullName, false, iconBytes)); continue; } } string json = Newtonsoft.Json.JsonConvert.SerializeObject(ls, Newtonsoft.Json.Formatting.Indented); System.IO.File.WriteAllText(@"d:\dotNetStructure.json", json, System.Text.Encoding.UTF8); System.Console.WriteLine(ls); } // End Sub Main
private static string getIconHash(string filename) { string iconHash = string.Empty; if (string.IsNullOrEmpty(filename)) { return(iconHash); } try { using (System.Drawing.Icon i = System.Drawing.Icon.ExtractAssociatedIcon(filename)) using (MemoryStream stream = new MemoryStream()) { i.Save(stream); iconHash = CryptoHelper.GetMd5String(stream.ToArray()); } } catch { } return(iconHash); }
public frmNewSpiraProject() { try { InitializeComponent(); //Set the title & icon. this.Title = Business.StaticFuncs.getCultureResource.GetString("strNewSpiraProject"); try { System.Drawing.Icon ico = (System.Drawing.Icon)Business.StaticFuncs.getCultureResource.GetObject("icoLogo"); MemoryStream icoStr = new MemoryStream(); ico.Save(icoStr); icoStr.Seek(0, SeekOrigin.Begin); this.Icon = BitmapFrame.Create(icoStr); } catch (Exception ex) { Logger.LogMessage(ex); } //Set initial colors and form status. this.barProg.Foreground = (Brush) new System.Windows.Media.BrushConverter().ConvertFrom(Business.StaticFuncs.getCultureResource.GetString("app_Colors_StyledBarNormal")); this.barProg.IsIndeterminate = false; this.barProg.Value = 0; this.grdAvailProjs.IsEnabled = false; this.grdEntry.IsEnabled = true; this.btnConnect.Tag = false; this.btnConnect.Click += new RoutedEventHandler(btnConnect_Click); int num = this.cmbProjectList.Items.Add("-- No Projects Available --"); this.cmbProjectList.SelectedIndex = num; this.cmbProjectList.SelectionChanged += new SelectionChangedEventHandler(cmbProjectList_SelectionChanged); } catch (Exception ex) { Logger.LogMessage(ex, "InitializeComponent()"); MessageBox.Show(StaticFuncs.getCultureResource.GetString("app_General_UnexpectedError"), StaticFuncs.getCultureResource.GetString("app_General_ApplicationShortName"), MessageBoxButton.OK, MessageBoxImage.Error); } }
public static Cursor CreateCursor(System.Drawing.Bitmap bmp, int xHotSpot, int yHotSpot) { using (var stream = new MemoryStream()) { // Save to .ico format using (System.Drawing.Icon icon = IconFromImage(bmp)) icon.Save(stream); // Convert saved file into .cur format stream.Seek(2, SeekOrigin.Begin); stream.WriteByte(2); stream.Seek(10, SeekOrigin.Begin); stream.WriteByte((byte)xHotSpot); stream.Seek(12, SeekOrigin.Begin); stream.WriteByte((byte)yHotSpot); stream.Seek(0, SeekOrigin.Begin); //File.WriteAllBytes("C:/PaintCursor.cur", stream.ToArray()); // Construct Cursor return(new Cursor(stream)); } }