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


C# Bitmap.MakeTransparent方法代码示例

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


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

示例1: hoge

 public Image[] hoge(string moji)
 {
     var il = new List<Image>();
     for (int i = 0; i < moji.Length; i++)
     {
         string c = moji[i].ToString();
         var sr = TextRenderer.MeasureText(_measure, c, _font, new Size(1024, 1024), TextFormatFlags.NoPadding);
         var bmp = new Bitmap((int)sr.Width, (int)sr.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
         var g = Graphics.FromImage(bmp);
         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
         TextRenderer.DrawText(g, c, _font, new Point(0, 0), Color.FromArgb(0, 0, 0), TextFormatFlags.NoPadding);
         var sr2 = g.MeasureString(moji, _font);
         bmp.MakeTransparent();
         il.Add(bmp);
     }
     return il.ToArray();
 }
开发者ID:tomohitoei,项目名称:cureplusProto,代码行数:17,代码来源:ScriptRenderer.cs

示例2: Load_Texture

    protected int Load_Texture(string filename, int xx, int yy)
    {
        int texture_id;

        Bitmap bitmap = new Bitmap(filename);
        bitmap.MakeTransparent(Color.Magenta);

        int px_x = (xx * TILE_W);
        int px_y = (yy * TILE_H);

        GL.GenTextures(1, out texture_id);
        GL.BindTexture(TextureTarget.Texture2D, texture_id);
        Check_for_GL_error("After calling GL.BindTexture()");

        Rectangle  rect = new Rectangle(px_x, px_y, TILE_W, TILE_H);
        BitmapData data = bitmap.LockBits(rect,
                                          ImageLockMode.ReadOnly,
                                          //System.Drawing.Imaging.PixelFormat.Format32bppPArgb);  // With this line uncommented, it works on XP and Ubuntu
                                          //System.Drawing.Imaging.PixelFormat.Format32bppRgb);  // This value worked on Windows 7, but on XP I see black pixels rather than transparent
                                          System.Drawing.Imaging.PixelFormat.Format32bppArgb);  // With this line uncommented, I get "scrambled" results on XP
        // Format32bppArgb was indicated by OpenTK.com user "Inertia", citing http://msdn.microsoft.com/en-us/library/8517ckds.aspx
        // However, rather than transparency, I observe a strange display (sending a screen shot)

        GL.TexImage2D(
            OpenTK.Graphics.OpenGL.TextureTarget.Texture2D,   // texture_target,
            0,                                                // level,
            OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgba,  // internal_format
            data.Width, data.Height,                          // width, height,
            0,                                                // border,
            OpenTK.Graphics.OpenGL.PixelFormat.Bgra,          // pixel_format
            OpenTK.Graphics.OpenGL.PixelType.UnsignedByte,    // pixel_type
            data.Scan0                                        // pixels
            );
        Check_for_GL_error("After calling GL.TexImage2D()");

        bitmap.UnlockBits(data);
        bitmap.Dispose();

        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Linear);

        return texture_id;
    }
开发者ID:sglasby,项目名称:OpenGL_Transparent_Sprite,代码行数:43,代码来源:OpenGL_Transparent_Sprite.cs

示例3: Main

    static void Main(string[] args)
    {
        Color maskcolor;

        if (args.Length == 2)
            maskcolor = Color.FromArgb(255, 0, 255);
        else if (args.Length == 3) {
            string[] channels = args[0].Split(',');

            maskcolor = Color.FromArgb(
                Convert.ToByte(channels[0]),
                Convert.ToByte(channels[1]),
                Convert.ToByte(channels[2])
            );
        }
        else {
            Console.WriteLine("syntax: infile.img outfile.img [maskcolor=255,0,255]");
            return;
        }

        Bitmap infile = new Bitmap(args[0]);
        infile.MakeTransparent(maskcolor);
        infile.Save(args[1]);
    }
开发者ID:kg,项目名称:XnaMannux,代码行数:24,代码来源:nopinko.cs

示例4: Load

    public static Texture2D Load(string name)
    {
        if (TexturesInRAM.ContainsKey(name))
            return TexturesInRAM[name];

        if (!File.Exists(Configuration.pTexture + name))
        {
            Debug.LogWarning("Текстура не найдена: " + name);
            return Texture2D.whiteTexture;
        }

        if (Path.GetExtension(Configuration.pTexture + name).ToLower() == ".bmp")
        {
            Bitmap Bitmap = new Bitmap(File.OpenRead(Configuration.pTexture + name), false);
            Texture2D Texture = new Texture2D(Bitmap.Width, Bitmap.Height, TextureFormat.RGBA32, true);

            if (Bitmap.PixelFormat.ToString() != "Format24bppRgb")
                Bitmap.MakeTransparent();

            using (var PngTexture = new MemoryStream())
            {
                Bitmap.Save(PngTexture, System.Drawing.Imaging.ImageFormat.Png);
                Texture.LoadImage(PngTexture.GetBuffer());
            }

            TexturesInRAM.Add(name, Texture);
            return Texture;
        }

        // https://gist.github.com/mikezila/10557162

        if (Path.GetExtension(Configuration.pTexture + name).ToLower() == ".tga")
        {
            BinaryReader BinReader = new BinaryReader(File.OpenRead(Configuration.pTexture + name));

            BinReader.BaseStream.Seek(12, SeekOrigin.Begin);

            short Width = BinReader.ReadInt16();
            short Height = BinReader.ReadInt16();
            int BitDepth = BinReader.ReadByte();

            // Skip a byte of header information we don't care about.
            BinReader.BaseStream.Seek(1, SeekOrigin.Current);

            Texture2D Texture = new Texture2D(Width, Height);
            Color32[] PulledColors = new Color32[Width * Height];

            if (BitDepth == 32)
            {
                for (int i = 0; i < Width * Height; i++)
                {
                    byte Red = BinReader.ReadByte();
                    byte Green = BinReader.ReadByte();
                    byte Blue = BinReader.ReadByte();
                    byte Alpha = BinReader.ReadByte();

                    PulledColors[i] = new Color32(Blue, Green, Red, Alpha);
                }
            }
            else if (BitDepth == 24)
            {
                for (int i = 0; i < Width * Height; i++)
                {
                    byte Red = BinReader.ReadByte();
                    byte Green = BinReader.ReadByte();
                    byte Blue = BinReader.ReadByte();

                    PulledColors[i] = new Color32(Blue, Green, Red, 1);
                }
            }
            else
            {
                throw new System.Exception("TGA texture had non 32/24 bit depth.");
            }

            Texture.SetPixels32(PulledColors);
            Texture.Apply();

            TexturesInRAM.Add(name, Texture);
            return Texture;
        }

        return Texture2D.whiteTexture;
    }
开发者ID:nkarpey,项目名称:Mafia-Resources-Loader,代码行数:84,代码来源:TextureLoader.cs

示例5: Scale9ImageTest

        public MFTestResults Scale9ImageTest()
        {
            Bitmap screen = new Bitmap(width, height);

            Bitmap btn = new Bitmap(WaterFallJpg.WaterFall, Bitmap.BitmapImageType.Jpeg);

            btn.MakeTransparent(ColorUtility.ColorFromRGB(255, 0, 255));

            screen.DrawRectangle(Color.White, 0, 0, 0, width, height, 0, 0, Color.White, 0, 0, 0, 0, 0, 256);

            screen.Scale9Image(30, 30, width / 3, height / 3, btn, 6, 6, 6, 6, 256);
            screen.Scale9Image(width / 2, height / 2, width / 3, 30, btn, 6, 6, 6, 6, 256);
            screen.Scale9Image(30, height / 2, 30, height / 3, btn, 6, 6, 6, 6, 256);
            screen.Scale9Image(width / 2, 30, 30, 30, btn, 6, 6, 6, 6, 256);

            screen.Flush();

            System.Threading.Thread.Sleep(500);

            screen.Dispose();
            btn.Dispose();

            return MFTestResults.Pass;
        }
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:24,代码来源:BitmapTest.cs

示例6: ImageDownscale

    private static void ImageDownscale(FileStream file, Media sender, string ext)
    {
        //Get settings
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(HttpContext.Current.Server.MapPath("~/config/WaffelAutoCompress.config"));
        var targetWidth = System.Convert.ToInt32(xmlDoc.GetElementsByTagName("targetwidth")[0].InnerText);
        var allowUpscale = xmlDoc.GetElementsByTagName("allowupscale")[0].InnerText;
        var propertyAlias = xmlDoc.GetElementsByTagName("propertyalias")[0].InnerText;
        var jpgQuality = xmlDoc.GetElementsByTagName("jpgquality")[0].InnerText;
        long compression = System.Convert.ToInt32(jpgQuality);
        string fullFilePath = HttpContext.Current.Server.MapPath(sender.getProperty(propertyAlias).Value.ToString());
        var fileNameWOExt = Path.GetFileNameWithoutExtension(file.Name.ToString());
        bool preserveType = xmlDoc.GetElementsByTagName("preservefiletype")[0].InnerText.ToString() == "true" && ext == "png";

        //Create new bitmap from uploaded file
        Bitmap originalBMP = new Bitmap(file);

        //Check if upscaling is allowed and if rule is relevant for current bitmap
        bool upscaleConflict = false;
        int longest = originalBMP.Width > originalBMP.Height ? originalBMP.Width : originalBMP.Height;
        if (allowUpscale != "true" && longest <= targetWidth) upscaleConflict = true;

        //If no upscale conflict is found proceed with image scaling and compression.
        if (!upscaleConflict)
        {
            //Set correct width and height for scaled image
            int imgWidth, imgHeight;
            if (originalBMP.Width > originalBMP.Height)
            {
                imgWidth = targetWidth;
                imgHeight = originalBMP.Height * targetWidth / originalBMP.Width;
            }

            else
            {
                imgWidth = originalBMP.Width * targetWidth / originalBMP.Height;
                imgHeight = targetWidth;
            }

            //Create scaled bitmap

            Bitmap imgBMP = new Bitmap(originalBMP, imgWidth, imgHeight);
            Graphics iGraphics = Graphics.FromImage(imgBMP);
            if ((ext == "png" || ext == "gif") && !preserveType) iGraphics.Clear(Color.White); //Sets white background for transparent png and gif
            //if (preserveType) iGraphics.Clear(Color.Transparent);
            iGraphics.SmoothingMode = SmoothingMode.HighSpeed; iGraphics.InterpolationMode = InterpolationMode.Default;
            iGraphics.DrawImage(originalBMP, 0, 0, imgWidth, imgHeight);

            System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
            eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, compression); //jpg compression

            file.Dispose();
            originalBMP.Dispose();
            //Save new scaled and compressed jpg
            string mediaFolderName = new DirectoryInfo(sender.getProperty(propertyAlias).Value.ToString()).Parent.Name;
            string targetDirectory = HttpContext.Current.Server.MapPath(string.Format("~/media/" + mediaFolderName + "/"));

            string originalFilePath = HttpContext.Current.Server.MapPath(sender.getProperty(propertyAlias).Value.ToString());

            //jpg if not preserved type
            if (!preserveType)
            {
                System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];
                imgBMP.Save(targetDirectory + fileNameWOExt + ".jpg", codec, eParams);
                sender.getProperty(propertyAlias).Value = "/media/" + mediaFolderName + "/" + fileNameWOExt + ".jpg";

                //Delete original file if not jpg
                if (ext != "jpg")
                {
                    File.Delete(originalFilePath);
                }
            }
            else
            {
                //ImageCodecInfo codec = GetEncoderInfo(ext);
                System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[4];
                imgBMP.MakeTransparent();
                imgBMP.Save(targetDirectory + fileNameWOExt + ".png", ImageFormat.Png);

            }

            imgBMP.Dispose();
            iGraphics.Dispose();

        }
        else
        {
            //If upscaleconflict is detected dispose and do nothing
            file.Dispose();
            originalBMP.Dispose();
        }
    }
开发者ID:eoutvik,项目名称:Waffel_AutoCompress,代码行数:92,代码来源:WaffelImageCompressor.cs


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