Click here to Skip to main content
15,889,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to create game application in j2me using canvas class
send to me source code
Posted

1 solution

Here's some source code:

C#
    private void BuildBitmap()
    {
        // load our files
        string camoPatternName = System.IO.Path.Combine(Application.StartupPath, "camomask_01.png");
        string outlineName     = System.IO.Path.Combine(Application.StartupPath, "ar15outline_01.png");
        string maskName        = System.IO.Path.Combine(Application.StartupPath, "ar15mask_01.png");
        Bitmap bmpPattern      = Bitmap.FromFile(camoPatternName) as Bitmap;
        Bitmap bmpOutline      = Bitmap.FromFile(outlineName) as Bitmap;
        Bitmap bmpMask         = Bitmap.FromFile(maskName) as Bitmap;
        Bitmap bmpResult       = new Bitmap(bmpPattern);
        // set the resolution of the result bmp
        bmpResult.SetResolution(bmpMask.HorizontalResolution, bmpMask.VerticalResolution);
        double percent = 40;
        //ReplaceColor(ref bmpResult, ColorTranslator.FromHtml("#FFFF00"), Color.Tan);
        //ReplaceColor(ref bmpResult, ColorTranslator.FromHtml("#00FF00"), Color.DarkGreen);
        //ReplaceColor(ref bmpResult, ColorTranslator.FromHtml("#0000FF"), Color.Brown);
        //ReplaceColor(ref bmpResult, ColorTranslator.FromHtml("#FF0000"), Color.DarkKhaki);
        Color[] oldColors = {ColorTranslator.FromHtml("#FFFF00"),
                             ColorTranslator.FromHtml("#00FF00"),
                             ColorTranslator.FromHtml("#0000FF"),
                             ColorTranslator.FromHtml("#FF0000")};
        //Color[] newColors =   {Color.Tan,
        //                     Color.DarkGreen,
        //                     Color.Brown,
        //                     Color.DarkKhaki};
        Color[] newColors = {Color.Tan,
                             Color.DarkGreen};
        ReplaceColors(ref bmpResult, oldColors, newColors);
        using (Graphics g = Graphics.FromImage(bmpResult))
        {
            g.CompositingMode = CompositingMode.SourceOver;
            g.DrawImageUnscaled(bmpMask, 0, 0);
            g.CompositingMode = CompositingMode.SourceOver;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.DrawImageUnscaled(bmpOutline, 0, 0);
            bmpResult.MakeTransparent(ColorTranslator.FromHtml("#FF00FF"));
        }
        ShowStep(bmpResult.Resize(percent));
        bmpPattern.Dispose();
        bmpOutline.Dispose();
        bmpMask.Dispose();
        bmpResult.Dispose();
    }
    /// <summary>
    /// GetPixel/SetPixel are so slow as to be distracting. This method is MUCH better.
    /// It uses a ColorMap object to define the color replacement, and can even be used
    /// to effect a single call to this method to replace the colors. SO COOL!
    /// </summary>
    /// <param name="bmp"></param>
    /// <param name="a"></param>
    /// <param name="b"></param>
    public void ReplaceColor(ref Bitmap bmp, Color a, Color b)
    {
        ColorMap Map = new ColorMap();
        Map.OldColor = a;
        Map.NewColor = b;
        ColorMap[] ReMapTable = { Map };
        ImageAttributes Attributes = new ImageAttributes();
        using (Graphics g = Graphics.FromImage(bmp))
        {
            Attributes.SetRemapTable(ReMapTable, ColorAdjustType.Bitmap);
            g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, Attributes);
        }
    }
    //--------------------------------------------------------------------------------
    /// <summary>
    /// Replaces all of the specified old colors with the specified new colors. This
    /// method also accomodates the ocurrence where there are more old colors than
    /// new colors.
    /// </summary>
    /// <param name="bmp"></param>
    /// <param name="oldColors"></param>
    /// <param name="newColors"></param>
    public void ReplaceColors(ref Bitmap bmp, Color[] oldColors, Color[] newColors)
    {
        ColorMap[] ReMapTable = new ColorMap[oldColors.Length];
        int newIndex = -1;
        //int oldIndex = -1;
        for (int  i = 0; i  < oldColors.Length; i++)
        {
            newIndex++;
            //oldIndex++;
            //if (oldIndex >= newColors.Length)
            if (i >= newColors.Length)
            {
                newIndex = 0;
            }
            ReMapTable[i] = new ColorMap(){OldColor = oldColors[i], NewColor = newColors[newIndex]};
        }
        ImageAttributes Attributes = new ImageAttributes();
        using (Graphics g = Graphics.FromImage(bmp))
        {
            Attributes.SetRemapTable(ReMapTable, ColorAdjustType.Bitmap);
            g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, Attributes);
        }
    }
    private void ShowStep(Image img)
    {
        pictureBox1.Image = null;
        pictureBox1.Image = img;
        pictureBox1.Refresh();
    }
}

public class MainClass {
    private List<Item> items;
    public List<Item> Items
    {
        get { return items; }
        set { items = value; }
    }
    public MainClass()
    {
        this.Items = new List<Item>();
    }
    private int id;
    public int Id {
        get {
            return id;
        }
        set {
            id = value;
        }
    }
}
public class Item {
    private string isc;
    public string IsC
    {
        get
        {
            return isc;
        }
        set
        {
            isc = value;
        }
    }
}


I've got more source code if you want it.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900