private void UpdateImageCodes()
 {
     for (int row = 0; row < 256; row++)
     {
         for (int column = 0; column < 256; column++)
         {
             ImageCodes[row, column] = OriginalImage.GetPixel(row, column).R;
         }
     }
 }
Exemple #2
0
 public void CreateGrayLevelImage()
 {
     for (int i = 0; i < Width; i++)
     {
         for (int j = 0; j < Height; j++)
         {
             int average = (OriginalImage.GetPixel(i, j).R + OriginalImage.GetPixel(i, j).G + OriginalImage.GetPixel(i, j).B) / 3;
             GrayLevelImage.SetPixel(i, j, Color.FromArgb(average, average, average));
         }
     }
 }
        public void ExecuteFilter(FilterOperation operation)
        {
            //Image imgg = new Bitmap(LogicImplementation.Properties.Resources.checkmateDealWithIt);
            //OriginalImage = new Bitmap(LogicImplementation.Properties.Resources.checkmateDealWithIt);

            if (transformImage == true)
            {
                for (int x = 0; x < OriginalImage.Height; x++)
                {
                    for (int y = 0; y < OriginalImage.Width; y++)
                    {
                        Color gotColor = OriginalImage.GetPixel(y, x);

                        filteredImage.SetPixel(y, x, operation(gotColor));
                    }
                    if (PartProcessed != null)
                    {
                        PartProcessed(((x * 100 / OriginalImage.Height)));
                    }
                }
            }
        }
        private void ArtistWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string ImagePath = (e.Argument as VaryObject).InPath;
            string ExcelPath = (e.Argument as VaryObject).OutPath;
            int    LastProgressPersent = -1, ProgressPercent = 0;

            //加载图像
            Bitmap OriginalImage;

            using (FileStream ImageStream = new FileStream(ImagePath, FileMode.Open))
                OriginalImage = Bitmap.FromStream(ImageStream) as Bitmap;
            if (OriginalImage == null)
            {
                throw new Exception("加载图片文件失败:" + ImagePath);
            }

            //缩小图像,太大了处理费时
            if (OriginalImage.Height > 100)
            {
                OriginalImage = new Bitmap(OriginalImage, new Size(100 * OriginalImage.Width / OriginalImage.Height, 100));
            }

            //创建表格
            CreateExcel(Path.GetFileNameWithoutExtension(ImagePath));

            /* 注意:
             * 查阅资料得知,[X,Y] 从1开始,不是0
             * Excel行高和列宽单位不统一,需要特殊设置
             * ( 基础设置:RowHeight=1 & ColumnWidth=0.1 )
             */
            range = range.Resize[OriginalImage.Width, OriginalImage.Height];

            if (ArtistWorker.CancellationPending)
            {
                e.Cancel = true; CloseExcel(); return;
            }
            //设置行高
            for (int Line = 1; Line <= OriginalImage.Height; Line++)
            {
                range.Rows[Line].RowHeight = 4.2;
            }

            if (ArtistWorker.CancellationPending)
            {
                e.Cancel = true; CloseExcel(); return;
            }
            //设置列宽
            for (int Column = 1; Column <= OriginalImage.Width; Column++)
            {
                range.Columns[Column].ColumnWidth = 0.4;
            }

            if (ArtistWorker.CancellationPending)
            {
                e.Cancel = true; CloseExcel(); return;
            }
            for (int Line = 0; Line < OriginalImage.Height; Line++)
            {
                for (int Column = 0; Column < OriginalImage.Width; Column++)
                {
                    //System.Diagnostics.Debug.Print("第 {0} 行, 第 {1} 列", Line, Column);
                    range.Cells[Line + 1, Column + 1].Interior.Color = OriginalImage.GetPixel(Column, Line);
                    if (ArtistWorker.CancellationPending)
                    {
                        e.Cancel = true; CloseExcel(); return;
                    }
                }

                ProgressPercent = 100 * Line / OriginalImage.Height;
                if (LastProgressPersent != ProgressPercent)
                {
                    LastProgressPersent = ProgressPercent;
                    //System.Diagnostics.Debug.Print("报告进度:" + ProgressPercent);
                    ArtistWorker.ReportProgress(LastProgressPersent);
                }
            }

            //if (ArtistWorker.CancellationPending) { e.Cancel = true; return; }
            //保存并关闭表格文件
            SaveAndCloseExcel(ExcelPath);
        }
        private void ArtistWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string ImagePath = (e.Argument as VaryObject).InPath;
            string ExcelPath = (e.Argument as VaryObject).OutPath;
            string WorkName = Path.GetFileNameWithoutExtension(ImagePath);
            int    LastProgressPersent = -1, ProgressPercent = 0;

            //加载图像
            Bitmap OriginalImage;

            using (FileStream ImageStream = new FileStream(ImagePath, FileMode.Open))
                OriginalImage = Bitmap.FromStream(ImageStream) as Bitmap;
            if (OriginalImage == null)
            {
                throw new Exception("加载图片文件失败:" + ImagePath);
            }

            //缩小图像,太大了处理费时
            if (OriginalImage.Height > 200)
            {
                OriginalImage = new Bitmap(OriginalImage, new Size(200 * OriginalImage.Width / OriginalImage.Height, 200));
            }

            using (ExcelPackage excel = new ExcelPackage(new FileInfo(ExcelPath)))
            {
                //设置文档属性
                OfficeProperties properties = excel.Workbook.Properties;
                properties.Author        = "Leon - Excel Artist";
                properties.Category      = "Leon - Excel Artist";
                properties.Comments      = "此表格由 ExcelArtist 生成,访问:https://github.com/CuteLeon/ExcelArtist";
                properties.Company       = "Leon - Excel Artist";
                properties.Created       = DateTime.Now;
                properties.HyperlinkBase = new Uri("https://github.com/CuteLeon/ExcelArtist");
                properties.Manager       = "Leon - Excel Artist";
                properties.Subject       = WorkName + " - Leon";
                properties.Title         = WorkName + " - Leon";
                if (ArtistWorker.CancellationPending)
                {
                    e.Cancel = true; return;
                }

                string ImageHash = OriginalImage.GetHashCode().ToString("X");
                //初始化表
                if (excel.Workbook.Worksheets.FirstOrDefault(s => s.Name == ImageHash) != null)
                {
                    excel.Workbook.Worksheets.Delete(ImageHash);
                }
                excel.Workbook.Worksheets.Add(ImageHash);
                ExcelWorksheet sheet = excel.Workbook.Worksheets[ImageHash];
                if (sheet == null)
                {
                    throw new Exception("创建 Sheet 失败");
                }

                /* 注意:
                 * 查阅资料得知,[X,Y] 从1开始,不是0
                 * Excel行高和列宽单位不统一,需要特殊设置
                 * ( 基础设置:RowHeight=1 & ColumnWidth=0.1 )
                 */

                //设置默认列宽和行高
                //sheet.DefaultColWidth = 0.7;
                //sheet.DefaultRowHeight = 4.2;
                if (ArtistWorker.CancellationPending)
                {
                    e.Cancel = true; return;
                }

                //开始核心任务
                using (ExcelRange range = sheet.Cells[1, 1, OriginalImage.Height, OriginalImage.Width])
                {
                    //设置列宽和行高
                    for (int Column = 0; Column < OriginalImage.Width; Column++)
                    {
                        sheet.Column(Column + 1).Width = 0.35;
                    }
                    ;
                    for (int Line = 0; Line < OriginalImage.Height; Line++)
                    {
                        sheet.Row(Line + 1).Height = 2.1;
                    }
                    if (ArtistWorker.CancellationPending)
                    {
                        e.Cancel = true; return;
                    }

                    for (int Line = 0; Line < OriginalImage.Height; Line++)
                    {
                        for (int Column = 0; Column < OriginalImage.Width; Column++)
                        {
                            //System.Diagnostics.Debug.Print("第 {0} 行, 第 {1} 列", Line, Column);
                            range[Line + 1, Column + 1].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                            range[Line + 1, Column + 1].Style.Fill.BackgroundColor.SetColor(OriginalImage.GetPixel(Column, Line));
                            if (ArtistWorker.CancellationPending)
                            {
                                e.Cancel = true; return;
                            }
                        }

                        ProgressPercent = 100 * Line / OriginalImage.Height;
                        if (LastProgressPersent != ProgressPercent)
                        {
                            LastProgressPersent = ProgressPercent;
                            //System.Diagnostics.Debug.Print("报告进度:" + ProgressPercent);
                            ArtistWorker.ReportProgress(LastProgressPersent);
                        }
                    }
                }
                if (ArtistWorker.CancellationPending)
                {
                    e.Cancel = true; return;
                }

                //保存文档
                excel.Save();
            }
        }