예제 #1
0
 public static byte[] ImageToBytes(string path)
 {
     System.Drawing.Image image = System.Drawing.Image.FromFile(path);
     System.Drawing.Imaging.ImageFormat format = image.RawFormat;
     using (MemoryStream ms = new MemoryStream())
     {
         if (format.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
         {
             image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
         }
         else if (format.Equals(System.Drawing.Imaging.ImageFormat.Png))
         {
             image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
         }
         else if (format.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
         {
             image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
         }
         else if (format.Equals(System.Drawing.Imaging.ImageFormat.Gif))
         {
             image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
         }
         else if (format.Equals(System.Drawing.Imaging.ImageFormat.Icon))
         {
             image.Save(ms, System.Drawing.Imaging.ImageFormat.Icon);
         }
         byte[] buffer = new byte[ms.Length];
         ms.Seek(0, SeekOrigin.Begin);
         ms.Read(buffer, 0, buffer.Length);
         return(buffer);
     }
 }
예제 #2
0
        private void btnSave_Image_Click(object sender, RoutedEventArgs e)
        {
            TimeSpan        scaleDuration     = new TimeSpan(0, 0, 0, 0, 1000);
            DoubleAnimation ProgressAnimation = new DoubleAnimation(0, 100, scaleDuration, FillBehavior.Stop);

            ProgressBar1.BeginAnimation(ProgressBar.ValueProperty, ProgressAnimation);


            DataTable dataTable = ds.Tables[0];

            foreach (DataRow row in dataTable.Rows)
            {
                if (row[0].ToString() == lvPictureResult.SelectedItem.ToString())
                {
                    //Store binary data read from the database in a byte array
                    byte[]       blob   = (byte[])row[1];
                    MemoryStream stream = new MemoryStream();
                    stream.Write(blob, 0, blob.Length);
                    stream.Position = 0;

                    System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
                    BitmapImage          bi  = new BitmapImage();
                    bi.BeginInit();

                    MemoryStream ms = new MemoryStream();
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                    ms.Seek(0, SeekOrigin.Begin);
                    bi.StreamSource = ms;
                    bi.EndInit();
                    image2.Source = bi;
                    //save file image to directory
                    try
                    {
                        if (img != null)
                        {
                            img.Save("..\\..\\PhotosSaved\\" + lvPictureResult.SelectedItem.ToString());
                            MessageBox.Show("Image saved successfully.");
                            Window1.Editor.ClearPhotoList();
                            Window1.Editor.Photos.Update();
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("There was a problem saving the file." +
                                        "Check the file permissions.");
                    }
                }
            }
        }
예제 #3
0
        private void btnShow_Click(object sender, RoutedEventArgs e)
        {
            DataTable dataTable = ds.Tables[0];

            foreach (DataRow row in dataTable.Rows)
            {
                if (row[0].ToString() == lvPictureResult.SelectedItem.ToString())
                {
                    //Store binary data read from the database in a byte array
                    byte[]       blob   = (byte[])row[1];
                    MemoryStream stream = new MemoryStream();
                    stream.Write(blob, 0, blob.Length);
                    stream.Position = 0;

                    System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
                    BitmapImage          bi  = new BitmapImage();
                    bi.BeginInit();

                    MemoryStream ms = new MemoryStream();
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                    ms.Seek(0, SeekOrigin.Begin);
                    bi.StreamSource = ms;
                    bi.EndInit();
                    image2.Source = bi;
                }
            }
        }
예제 #4
0
 private byte[] ImageToByteArray(System.Drawing.Image i)
 {
     using (var ms = new MemoryStream())
     {
         i.Save(ms, i.RawFormat);
         return(ms.ToArray());
     }
 }
예제 #5
0
        /// <summary>
        /// 加载图片
        /// </summary>
        public void LoadImages()
        {
            images = new List <FileInfo>();

            var commonPicturesDir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures));

            images.AddRange(commonPicturesDir.GetFiles("*.jpg", SearchOption.AllDirectories));
            images.AddRange(commonPicturesDir.GetFiles("*.png", SearchOption.AllDirectories));


            var myPicturesDir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));

            images.AddRange(myPicturesDir.GetFiles("*.jpg", SearchOption.AllDirectories));
            images.AddRange(myPicturesDir.GetFiles("*.png", SearchOption.AllDirectories));

            myMusicDir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic));
            myCoverDir = new DirectoryInfo(myMusicDir.FullName + @"\Covers\");
            if (!myCoverDir.Exists)
            {
                myCoverDir.Create();
            }
            myLyricDir = new DirectoryInfo(myMusicDir.FullName + @"\Lyrics\");
            if (!myLyricDir.Exists)
            {
                myLyricDir.Create();
            }
            FileInfo[] musics = myMusicDir.GetFiles("*.mp3");

            foreach (var music in musics)
            {
                string cover = myCoverDir + @"\" + System.IO.Path.GetFileNameWithoutExtension(music.FullName) + ".jpg";
                if (!File.Exists(cover))
                {
                    ID3Info info = new ID3Info(music.FullName, true);//从ID3V2信息中读取封面
                    info.Load();
                    if (info.ID3v2Info.HaveTag && info.ID3v2Info.AttachedPictureFrames.Items.Length > 0)
                    {
                        MemoryStream         ms  = info.ID3v2Info.AttachedPictureFrames.Items[0].Data;
                        System.Drawing.Image img = Bitmap.FromStream(ms);
                        img.Save(cover, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                    else
                    {
                        File.Copy(System.Environment.CurrentDirectory + @"\cover.jpg", cover, true);
                    }
                }
                images.Add(new FileInfo(cover));
            }

            images.Sort(new FileInfoComparer());
            foreach (FileInfo f in images)
            {
                flow.Add(Environment.MachineName, f.FullName);
            }
        }
예제 #6
0
        public void GuardarImagen(string nombre)
        {
            string dondeG = Efectos.AssemblyDirectory + @"\Imagenes";

            System.IO.DirectoryInfo Dire = new System.IO.DirectoryInfo(dondeG);
            if (!Dire.Exists)
            {
                Dire.Create();
            }

            System.Drawing.Image Img = UltimoBitmap;
            Img.Save(dondeG + @"\" + nombre + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }
예제 #7
0
 //Convert Image/Bitmap to ImageSource
 public ImageSource ConvertFromImage(System.Drawing.Image image)
 {
     using (var ms = new MemoryStream())
     {
         bitmapImage = new BitmapImage();
         image.Save(ms, ImageFormat.Bmp);
         ms.Seek(0, SeekOrigin.Begin);
         bitmapImage.BeginInit();
         bitmapImage.CacheOption  = BitmapCacheOption.OnLoad;
         bitmapImage.StreamSource = ms;
         bitmapImage.EndInit();
         ms.Flush();
         return(bitmapImage);
     }
 }
예제 #8
0
        private static BitmapImage BitmapToImageSource(System.Drawing.Image bitmap)
        {
            using (var memory = new MemoryStream())
            {
                bitmap.Save(memory, ImageFormat.Png);
                memory.Position = 0;
                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption  = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();

                return(bitmapImage);
            }
        }
        public static ImageSource GetImageSource(System.Drawing.Image s)
        {
            ImageSource result = null;

            using (var memory = new MemoryStream())
            {
                s.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
                memory.Position = 0;
                var bitmapimage = new BitmapImage();
                bitmapimage.BeginInit();
                bitmapimage.StreamSource = memory;
                bitmapimage.CacheOption  = BitmapCacheOption.OnLoad;
                bitmapimage.EndInit();

                result = bitmapimage;
            }
            return(result);
        }
예제 #10
0
        private void Capturando(object sender, NewFrameEventArgs eventArgs)
        {
            try
            {
                System.Drawing.Image img = (Bitmap)eventArgs.Frame.Clone();

                MemoryStream ms = new MemoryStream();
                img.Save(ms, ImageFormat.Bmp);
                ms.Seek(0, SeekOrigin.Begin);
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = ms;
                bi.EndInit();

                bi.Freeze();
                Dispatcher.BeginInvoke(new ThreadStart(delegate
                {
                    img1.Source = bi;
                }));
            }
            catch (Exception ex)
            {
            }
        }
        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            string        mylist    = (" ");
            List <string> uriResult = new List <string>();
            int           iterator;
            List <string> boldTexts = new List <string>();

            foreach (Paragraph p in rchTextbox.Document.Blocks)
            {
                foreach (var inline in p.Inlines)
                {
                    if (inline.FontWeight == FontWeights.Bold)
                    {
                        var textRange = new TextRange(inline.ContentStart, inline.ContentEnd);
                        boldTexts.Add(textRange.Text);
                        //MessageBox.Show(textRange.Text);
                        mylist += (textRange.Text + " ");
                    }
                    myWeb.Source = new Uri("https://www.google.com/search?tbm=isch&q=" + titleWord.Text + mylist);

                    string urls = (" ");
                    //create web client
                    WebClient googleImages = new WebClient();
                    //This regex searches for image urls in the html from google
                    Regex googleRegex = new Regex(@"""https://[^""]*""", RegexOptions.Compiled | RegexOptions.IgnoreCase);

                    //get google html for image search
                    string          html          = googleImages.DownloadString("https://www.google.com/search?tbm=isch&q=" + titleWord.Text + mylist);
                    MatchCollection googleMatches = googleRegex.Matches(html);

                    //MessageBox.Show("" + googleMatches.Count);
                    foreach (Match m in googleMatches)
                    {
                        //add the match to the arraylist
                        urls += (m.Value + " ");
                    }

                    string[] imgInfo = urls.Split(' ');

                    foreach (string info in imgInfo.Where((info => !string.IsNullOrEmpty(info))))
                    {
                        //create the image and add it to the List
                        foreach (string s in info.Split(' '))
                        {
                            if (!String.IsNullOrEmpty(s))
                            {
                                uriResult.Add(s);
                            }
                        }
                    }
                    iterator = 1;
                    string bmp         = uriResult.ElementAt(iterator);
                    Image  googleImage = new Image();
                    googleImage.Name   = "image";
                    googleImage.Source = new BitmapImage(new Uri(bmp.Substring(1, bmp.Length - 5)));



                    if (iterator < 7)
                    {
                        iterator++;
                    }
                    ;


                    string bnmp1 = uriResult.ElementAt(1);
                    button1.Content = new Image

                    {
                        Source = new BitmapImage(new Uri(bnmp1.Substring(1, bnmp1.Length - 5)))
                    };

                    string bnmp2 = uriResult.ElementAt(2);
                    button2.Content = new Image
                    {
                        Source = new BitmapImage(new Uri(bnmp2.Substring(1, bnmp2.Length - 5)))
                    };


                    string bnmp3 = uriResult.ElementAt(3);
                    button3.Content = new Image
                    {
                        Source = new BitmapImage(new Uri(bnmp3.Substring(1, bnmp3.Length - 5)))
                    };


                    string bnmp4 = uriResult.ElementAt(4);
                    button4.Content = new Image
                    {
                        Source = new BitmapImage(new Uri(bnmp4.Substring(1, bnmp4.Length - 5)))
                    };


                    string bnmp5 = uriResult.ElementAt(5);
                    button5.Content = new Image
                    {
                        Source = new BitmapImage(new Uri(bnmp5.Substring(1, bnmp5.Length - 5)))
                    };


                    string bnmp6 = uriResult.ElementAt(6);
                    button6.Content = new Image
                    {
                        Source = new BitmapImage(new Uri(bnmp6.Substring(1, bnmp6.Length - 5)))
                    };

                    System.Drawing.Image image = DownloadImageFromUrl(bnmp1.Substring(1, bnmp1.Length - 5));
                    string rootPath            = @"C:/ImagesForPowerPoint/ImagesForPowerPoint/Images/";
                    string fileName            = System.IO.Path.Combine(rootPath, "image1.png");
                    image.Save(fileName);
                    image.Dispose();

                    System.Drawing.Image image2 = DownloadImageFromUrl(bnmp2.Substring(1, bnmp2.Length - 5));
                    string rootPath2            = @"C:/ImagesForPowerPoint/ImagesForPowerPoint/Images/";
                    string fileName2            = System.IO.Path.Combine(rootPath2, "image2.png");
                    image2.Save(fileName2);
                    image2.Dispose();

                    System.Drawing.Image image3 = DownloadImageFromUrl(bnmp3.Substring(1, bnmp3.Length - 5));
                    string rootPath3            = @"C:/ImagesForPowerPoint/ImagesForPowerPoint/Images/";
                    string fileName3            = System.IO.Path.Combine(rootPath3, "image3.png");
                    image3.Save(fileName3);
                    image3.Dispose();

                    System.Drawing.Image image4 = DownloadImageFromUrl(bnmp4.Substring(1, bnmp4.Length - 5));
                    string rootPath4            = @"C:/ImagesForPowerPoint/ImagesForPowerPoint/Images/";
                    string fileName4            = System.IO.Path.Combine(rootPath4, "image4.png");
                    image4.Save(fileName4);
                    image4.Dispose();

                    System.Drawing.Image image5 = DownloadImageFromUrl(bnmp5.Substring(1, bnmp5.Length - 5));
                    string rootPath5            = @"C:/ImagesForPowerPoint/ImagesForPowerPoint/Images/";
                    string fileName5            = System.IO.Path.Combine(rootPath5, "image5.png");
                    image5.Save(fileName5);
                    image5.Dispose();

                    System.Drawing.Image image6 = DownloadImageFromUrl(bnmp6.Substring(1, bnmp6.Length - 5));
                    string rootPath6            = @"C:/ImagesForPowerPoint/ImagesForPowerPoint/Images/";
                    string fileName6            = System.IO.Path.Combine(rootPath6, "image6.png");
                    image6.Save(fileName6);
                    image6.Dispose();
                }
            }
        }