当前位置: 首页>>代码示例>>C#>>正文


C# System.Drawing.Bitmap.SetPixel方法代码示例

本文整理汇总了C#中System.Drawing.Bitmap.SetPixel方法的典型用法代码示例。如果您正苦于以下问题:C# System.Drawing.Bitmap.SetPixel方法的具体用法?C# System.Drawing.Bitmap.SetPixel怎么用?C# System.Drawing.Bitmap.SetPixel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Drawing.Bitmap的用法示例。


在下文中一共展示了System.Drawing.Bitmap.SetPixel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MakeImagesNegative

        static void MakeImagesNegative(string sourceDirectoryPath, string targetDirectoryPath)
        {
            string[] filenames = System.IO.Directory.GetFiles(sourceDirectoryPath);

            foreach (var filename in filenames)
            {
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(filename);
                Parallel.For(0, bitmap.Height, (bitmapRowIndex) =>
                {
                    lock (bitmap)
                    {
                        for (int bitmapColIndex = 0; bitmapColIndex < bitmap.Width; bitmapColIndex++)
                        {

                            var pixel = bitmap.GetPixel(bitmapColIndex, bitmapRowIndex);
                            var negativePixel = System.Drawing.Color.FromArgb(
                                byte.MaxValue - pixel.A,
                                byte.MaxValue - pixel.R,
                                byte.MaxValue - pixel.G,
                                byte.MaxValue - pixel.B);

                            bitmap.SetPixel(bitmapColIndex, bitmapRowIndex, negativePixel);
                        }
                    }
                });

                bitmap.Save(filename.Replace(sourceDirectoryPath, targetDirectoryPath));
            }
        }
开发者ID:TelerikAcademy,项目名称:TelerikAcademyPlus,代码行数:29,代码来源:Program.cs

示例2: DrawBinaryImage

        //BinaryWrite方法将一个二进制字符串写入HTTP输出流
        //public void BinaryWrite(byte[] buffer)
        public void DrawBinaryImage()
        {
            //要绘制的字符串
            string word = "5142";

            //设定画布大小,Math.Ceiling向上取整
            int width = int.Parse(Math.Ceiling(word.Length * 20.5).ToString());
            int height = 22;

            //创建一个指定大小的画布
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
            //创建一个指定的GDI+绘图图面
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
            try
            {
                //用白色填充图面
                g.Clear(System.Drawing.Color.White);

                //绘制背景噪音线
                Random r = new Random();
                int x1, x2, y1, y2;
                for (int i = 0; i < 2; i++)
                {
                    x1 = r.Next(image.Width);
                    x2 = r.Next(image.Width);
                    y1 = r.Next(image.Height);
                    y2 = r.Next(image.Height);
                    g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Black), x1, y1, x2, y2);
                }

                //绘制文字
                System.Drawing.Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush
                                                        (new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
                                                        System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 5.2f, true);
                g.DrawString(word, font, brush, 2, 2);

                //绘制前景噪点
                int x, y;
                for (int i = 0; i < 100; i++)
                {
                    x = r.Next(image.Width);
                    y = r.Next(image.Height);
                    image.SetPixel(x, y, System.Drawing.Color.FromArgb(r.Next()));
                }

                //绘制边框
                g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                System.IO.MemoryStream ms = new MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                Response.ClearContent();
                Response.ContentType = "image/Gif";
                Response.BinaryWrite(ms.ToArray());
            }
            catch (Exception)
            {
                g.Dispose();
                image.Dispose();
            }
        }
开发者ID:ZhuangChen,项目名称:CSharpBasicTraining,代码行数:62,代码来源:04Built-in_Object.aspx.cs

示例3: TC_ShouldDeserializePNGBase64Image

        public void TC_ShouldDeserializePNGBase64Image() {
            using (var bitmap = new System.Drawing.Bitmap(1024 * 2, 768 * 2)) {
                bitmap.SetPixel(1, 1, System.Drawing.Color.Bisque);
                bitmap.SetPixel(2, 2, System.Drawing.Color.Honeydew);
                bitmap.SetPixel(3, 3, System.Drawing.Color.PapayaWhip);
                Image input = new Image(bitmap);

                var ms = new MemoryStream();
                bitmap.Save(ms, ImageFormat.Png);
                byte[] expected = ms.ToArray();
                string str = Convert.ToBase64String(expected);
                Image output = (Image)SerializeAndDeserialize(str);

                A.AreEqual(input.CRC, output.CRC);
            }
        }
开发者ID:florentbr,项目名称:SeleniumBasic,代码行数:16,代码来源:TS_Serialiser.cs

示例4: draw2

        private static void draw2()
        {
            string output_filename = "D:\\colorwheel.png";

            int padding = 10;
            int inner_radius = 200;
            int outer_radius = inner_radius + 50;

            int bmp_width = (2 * outer_radius) + (2 * padding);
            int bmp_height = bmp_width;

            var center = new System.Drawing.Point(bmp_width / 2, bmp_height / 2);
            var c = System.Drawing.Color.Red;

            using (var bmp = new System.Drawing.Bitmap(bmp_width, bmp_height))
            {
                using (var g = System.Drawing.Graphics.FromImage(bmp))
                {
                    g.FillRectangle(System.Drawing.Brushes.White, 0, 0, bmp.Width, bmp.Height);
                }
                for (int y = 0; y < bmp_width; y++)
                {
                    int dy = (center.Y - y);

                    for (int x = 0; x < bmp_width; x++)
                    {
                        int dx = (center.X - x);

                        double dist = System.Math.Sqrt(dx * dx + dy * dy);


                        if (dist >= inner_radius && dist <= outer_radius)
                        {
                            double theta = System.Math.Atan2(dy, dx);
                            // theta can go from -pi to pi

                            double hue = (theta + System.Math.PI) / (2 * System.Math.PI);

                            // HSV -> RGB
                            const double sat = 1.0;
                            const double val = 1.0;
                            c = HSVToRGB2(hue, sat, val);

                            // HSL -> RGB
                            // const double sat = 1.0;
                            // const double light = 0.5;
                            // c = HSLToRGB2(hue, sat, light);
                           

                            bmp.SetPixel(x, y, c);
                        }
                    }
                }
                bmp.Save(output_filename);
            }
        }
开发者ID:saveenr,项目名称:saveenr,代码行数:56,代码来源:Program.cs

示例5: Execute

        public static int Execute( List<string> args )
        {
            if ( args.Count < 1 ) {
                Console.WriteLine( "Usage: infile.g1t" );
                return -1;
            }

            string infile = args[0];
            string outdir = Path.GetDirectoryName( infile );
            string filename = Path.GetFileName( infile );

            var g1t = new g1t( infile );

            for ( int i = 0; i < g1t.Textures.Count; ++i ) {
                var tex = g1t.Textures[i];

                if ( Textures.DDSHeader.IsDDSTextureFormat( tex.Format ) ) {
                    string path = System.IO.Path.Combine( outdir, filename + "_" + i.ToString( "D4" ) + ".dds" );
                    using ( var fs = new System.IO.FileStream( path, System.IO.FileMode.Create ) ) {
                        fs.Write( Textures.DDSHeader.Generate( tex.Width, tex.Height, tex.Mipmaps, tex.Format ) );
                        fs.Write( tex.Data );
                        fs.Close();
                    }
                } else {
                    for ( int mipmapLevel = 0; mipmapLevel < tex.Mipmaps; ++mipmapLevel ) {
                        string path = System.IO.Path.Combine( outdir, filename + "_" + i.ToString( "D4" ) + ( mipmapLevel > 0 ? "_Mipmap" + mipmapLevel : "" ) + ".png" );
                        int width = (int)tex.Width / ( 1 << mipmapLevel );
                        int height = (int)tex.Height / ( 1 << mipmapLevel );

                        var bitmap = new System.Drawing.Bitmap( width, height );
                        long offset = tex.GetDataStart( mipmapLevel );
                        for ( int j = 0; j < tex.GetDataLength( mipmapLevel ) / 4; ++j ) {
                            long idx = offset + j * 4;
                            System.Drawing.Color color;
                            switch ( tex.Format ) {
                                case Textures.TextureFormat.RGBA:
                                    color = System.Drawing.Color.FromArgb( tex.Data[idx + 3], tex.Data[idx + 2], tex.Data[idx + 1], tex.Data[idx] );
                                    break;
                                case Textures.TextureFormat.ABGR:
                                    color = System.Drawing.Color.FromArgb( tex.Data[idx], tex.Data[idx + 3], tex.Data[idx + 2], tex.Data[idx + 1] );
                                    break;
                                default:
                                    throw new Exception( "Unsupported texture format in png generation color loop." );
                            }
                            bitmap.SetPixel( (int)( j % width ), (int)( j / width ), color );
                        }
                        bitmap.Save( path, System.Drawing.Imaging.ImageFormat.Png );
                    }
                }
            }

            return 0;
        }
开发者ID:AdmiralCurtiss,项目名称:HyoutaTools,代码行数:53,代码来源:DDSConverter.cs

示例6: GetMaskIPictureDispFromBitmap

 public static stdole.IPictureDisp GetMaskIPictureDispFromBitmap(System.Drawing.Bitmap bmp)
 {
     System.Drawing.Bitmap mask = new System.Drawing.Bitmap(bmp);
     try
     {
         for (int x = 0; x < mask.Width; x++)
         {
             for (int y = 0; y < mask.Height; y++)
             {
                 if (mask.GetPixel(x, y).A == 0)
                     mask.SetPixel(x, y, System.Drawing.Color.White);
                 else
                     mask.SetPixel(x, y, System.Drawing.Color.Black);
             }
         }
         return GetIPictureDispFromImage(mask);
     }
     finally
     {
         mask.Dispose();
     }
 }
开发者ID:japj,项目名称:bidshelper,代码行数:22,代码来源:ImageToPictureDispConverter.cs

示例7: CreateFromImage

 public static MemoryStream CreateFromImage(Image im)
 {
     System.Drawing.Bitmap bm = new System.Drawing.Bitmap(im.HRes, im.VRes);
     for (int x = 0; x < im.HRes; x++)
     {
         for (int y = 0; y < im.VRes; y++)
         {
             Vector3 v = im.Pixels[x + y * im.HRes];
             bm.SetPixel(x, y, System.Drawing.Color.FromArgb((int)(v.X * 255.0f), (int)(v.Y * 255.0f), (int)(v.Z * 255.0f)));
         }
     }
     MemoryStream ms = new MemoryStream();
     bm.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
     return ms;
 }
开发者ID:JointJBA,项目名称:DisqueEngine,代码行数:15,代码来源:Extensions.cs

示例8: BitmapFormat

 void BitmapFormat(string path)
 {
     System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(path);
     if (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
         return;
     System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
     for (int y = 0; y < newBitmap.Height; y++)
         for (int x = 0; x < newBitmap.Width; x++)
             newBitmap.SetPixel(x, y, bitmap.GetPixel(x, y));
     bitmap.Dispose();
     if (File.Exists(path))
         File.Delete(path);
     newBitmap.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
     newBitmap.Dispose();
 }
开发者ID:bitnick1000,项目名称:ConvertPictureFormats,代码行数:15,代码来源:MainWindow.xaml.cs

示例9: WindowProc

        void WindowProc()
        {
            f = new PaintableForm(mem);

            var g = f.CreateGraphics();
            var font_bmp = new System.Drawing.Bitmap(8 * 256, 16,
                g);
            g.Dispose();

            // Load up the font data
            var fs = new System.IO.FileStream("C:\\Users\\jncro\\Documents\\fpga\\vga\\font.hex",
                System.IO.FileMode.Open, System.IO.FileAccess.Read);
            var sr = new System.IO.StreamReader(fs);
            while(sr.EndOfStream == false)
            {
                string line = sr.ReadLine();
                if (!line.StartsWith(":01"))
                    break;

                uint addr = uint.Parse(line.Substring(3, 4),
                    System.Globalization.NumberStyles.HexNumber);
                uint val = uint.Parse(line.Substring(9, 2),
                    System.Globalization.NumberStyles.HexNumber);

                int char_idx = (int)(addr / 16);
                int char_row = (int)(addr % 16);

                for(int i = 0; i < 8; i++)
                {
                    uint bit_val = (val >> i) & 0x1;
                    System.Drawing.Color c;
                    if (bit_val == 0)
                        c = System.Drawing.Color.Black;
                    else
                        c = System.Drawing.Color.White;

                    font_bmp.SetPixel(char_idx * 8 + i, char_row, c);
                }
            }
            fs.Close();


            f.f = font_bmp;
            
            f.Show();

            Application.Run();
        }
开发者ID:jncronin,项目名称:jca,代码行数:48,代码来源:Vga.cs

示例10: MainWindow

        public MainWindow()
        {
            InitializeComponent();

            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            // Set filter options and filter index.
            openFileDialog1.Filter = "All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.Multiselect = true;

            // Call the ShowDialog method to show the dialog box.
            bool? userClickedOK = openFileDialog1.ShowDialog();

            // Process input if the user clicked OK.
            if (userClickedOK == true)
            {
                AllocConsole();
                //Run alg

                Console.WriteLine("Load original...");
                System.Drawing.Bitmap org = new System.Drawing.Bitmap(openFileDialog1.FileName);
                Original = org.Clone(new System.Drawing.Rectangle(0, 0, org.Width, org.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                //Convert to grayscale
                Console.WriteLine("Convert original to grayscale");
                for (int i = 0; i < Original.Width; i++)
                {
                    for (int x = 0; x < Original.Height; x++)
                    {
                        System.Drawing.Color oc = Original.GetPixel(i, x);
                        int grayScale = (int)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11));
                        System.Drawing.Color nc = System.Drawing.Color.FromArgb(oc.A, grayScale, grayScale, grayScale);
                        Original.SetPixel(i, x, nc);
                    }
                }

                PerformAlgorithm();

                this.ImgOrg.Source = BitmapToImageSource(Original);
               // FreeConsole();
            }
            else
            {
                this.Close();
            }
        }
开发者ID:Lowhuhn,项目名称:LaserControl,代码行数:48,代码来源:MainWindow.xaml.cs

示例11: GetAndSetPixelComparingToSystemBitmap

        public void GetAndSetPixelComparingToSystemBitmap()
        {
            using (var systemBitmap = new System.Drawing.Bitmap(DefaultWidth, DefaultHeight))
            {
                int x = 0;
                int y = 0;

                systemBitmap.SetPixel(x, y, System.Drawing.Color.FromArgb(DefaultColor.ARGB));
                bmp.Set(x, y, new Color(DefaultColorAsInt));

                var retrivedColor = systemBitmap.GetPixel(x, y);
                var retrievedColorAsInt = bmp.Get(x, y);

                Assert.AreEqual(retrivedColor.ToArgb(), retrievedColorAsInt.ARGB);
            }
        }
开发者ID:Mirandatz,项目名称:Trauer,代码行数:16,代码来源:BitmapTests.cs

示例12: RenderImageFromScreen

 public static string RenderImageFromScreen(PixelScreen screen, int height, int width, string folder, int framenumber)
 {
     System.Drawing.Bitmap bm = new System.Drawing.Bitmap(width, height);
     for (int i = 0; i < screen.Pixels.Count; i++)
     {
         Pixel p = screen.Pixels[i];
         Color r = Color.FromScRgb(1.0f, p.Color.R, p.Color.G, p.Color.B);
         bm.SetPixel((int)p.X, (int)p.Y, System.Drawing.Color.FromArgb(r.R, r.G, r.B));
     }
     if (!Directory.Exists(@"C:\Users\Belal\Pictures\Movies\" + folder))
     {
         Directory.CreateDirectory(@"C:\Users\Belal\Pictures\Movies\" + folder);
     }
     string file = @"C:\Users\Belal\Pictures\Movies\" + folder + @"\" + framenumber + ".png";
     bm.Save(file, System.Drawing.Imaging.ImageFormat.Png);
     return file;
 }
开发者ID:JointJBA,项目名称:DisqueEngine,代码行数:17,代码来源:ScreenImage.xaml.cs

示例13: ToDColorBmp

        private Bitmap ToDColorBmp(Color[] data, int width, int height)
        {
            Bitmap bmp = new Bitmap(width, height);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int i = ((y * width) + x);
                    Color oC = data[i];
                    DColor nC = DColor.FromArgb(oC.A, oC.R, oC.G, oC.B);

                    bmp.SetPixel(x, y, nC);
                }
            }

            return bmp;
        }
开发者ID:MentulaGames,项目名称:XnaGuiItems,代码行数:18,代码来源:ImagedGuiItem.cs

示例14: RenderImage

 public void RenderImage(World screen)
 {
     System.Drawing.Bitmap bm = new System.Drawing.Bitmap(screen.ViewPlane.HRes, screen.ViewPlane.VRes);
     for (int i = 0; i < screen.Screen.Pixels.Count; i++)
     {
         Pixel p = screen.Screen.Pixels[i];
         Color r = Color.FromScRgb(1.0f, p.Color.R, p.Color.G, p.Color.B);
         bm.SetPixel((int)p.X, (int)p.Y, System.Drawing.Color.FromArgb(r.R, r.G, r.B));
     }
     string name = DateTime.Now.Millisecond.ToString() + ".png";
     while (File.Exists(name))
     {
         name = DateTime.Now.Millisecond.ToString() +".png";
     }
     string file = @"C:\Users\Belal\Pictures\3D\" + name;
     bm.Save(file, System.Drawing.Imaging.ImageFormat.Png);
     collection.Clear();
     collection.Add(new Image() { Source = new BitmapImage(new Uri(file)), Height = bm.Height, Width = bm.Width });
 }
开发者ID:JointJBA,项目名称:DisqueEngine,代码行数:19,代码来源:Screen.cs

示例15: GetSpectorImageBrush

        private System.Windows.Media.ImageBrush GetSpectorImageBrush()
        {
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(passengerDensity.GetLength(0), passengerDensity.GetLength(1));
            int max = 0;
            for (int i = 0; i < passengerDensity.GetLength(0); i++)
            {
                for (int j = 0; j < passengerDensity.GetLength(1); j++)
                {
                    if (max < passengerDensity[i, j])
                    {
                        max = passengerDensity[i, j];
                    }
                }
            }

            for (int i = 0; i < passengerDensity.GetLength(0); i++)
            {
                for (int j = 0; j < passengerDensity.GetLength(1); j++)
                {
                    double value = (double)passengerDensity[i, j] / max;
                    if (value < 0.2)
                    {
                        bmp.SetPixel(i, j, System.Drawing.Color.FromArgb(128, 255 - Convert.ToInt32(255 * (value == 0 ? value : value + 0.3)), 255, 255 - Convert.ToInt32(255 * (value == 0 ? value : value + 0.3))));
                    }
                    else if (value < 0.6)
                    {
                        bmp.SetPixel(i, j, System.Drawing.Color.FromArgb(128, 255 - Convert.ToInt32(255 * (value + 0.1)), 255 - Convert.ToInt32(255 * (value + 0.1)), 255));
                    }
                    else
                    {
                        bmp.SetPixel(i, j, System.Drawing.Color.FromArgb(128, 255, 255 - Convert.ToInt32(255 * value), 255 - Convert.ToInt32(255 * value)));
                    }
                }
            }
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            ms.Position = 0;
            System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage();
            image.BeginInit();
            image.StreamSource = ms;
            image.EndInit();
            return new System.Windows.Media.ImageBrush(image);
        }
开发者ID:nomoreserious,项目名称:FlowSimulation,代码行数:43,代码来源:wndSpectralDensity.xaml.cs


注:本文中的System.Drawing.Bitmap.SetPixel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。