C#, Android: Create bitmap with single color












1















I want to create a bitmap that is filled with only one color (#eeeeee).
I managed to create a bitmap and set it to its viewholder but I cannot seem to find the right way to fill it entierely with my desired color. The output is always only a white rectangle. But it needs to be greyish as in #eeeeee. This is my effort:



            Bitmap greyBitmap = Bitmap.CreateBitmap(1000, 500, Bitmap.Config.Argb8888);
Bitmap mutableBitmap = greyBitmap.Copy(Bitmap.Config.Argb8888, true);//<–true makes copy mutable
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
paint.Color = new Color(Color.ParseColor("#eeeeee"));
canvas.DrawRect(1000, 500, 1000, 500, paint);
vh.dr = new BitmapDrawable(mutableBitmap);


Chances are, I am taking too many steps... Feel free to correct me on this.










share|improve this question




















  • 1





    The coordinates you have in DrawRect() are off. The first two should be 0, 0. Anyway, there's a much simpler way to do that. Just call eraseColor() with your color value on the mutable Bitmap.

    – Mike M.
    Nov 23 '18 at 10:47













  • oh yesm thanks that did the trick. however, would you mind posting both remarks as an answer that i could accept? thank you!

    – Innomotion Media
    Nov 23 '18 at 10:51






  • 1





    Oh, I'm good. Just a couple simple suggestions. :-) Please feel free to post your own answer. This would be a handy duplicate target in the future for users asking specifically for C# code. Btw, I just double checked, and that particular Bitmap.CreateBitmap() overload returns a mutable Bitmap already, so there's no need to copy it first. You can do the whole thing in two lines. Anyhoo, thanks for the offer. I appreciate it. Glad you got it working. Cheers!

    – Mike M.
    Nov 23 '18 at 11:14











  • your wish shall be my command ;)

    – Innomotion Media
    Nov 23 '18 at 11:18
















1















I want to create a bitmap that is filled with only one color (#eeeeee).
I managed to create a bitmap and set it to its viewholder but I cannot seem to find the right way to fill it entierely with my desired color. The output is always only a white rectangle. But it needs to be greyish as in #eeeeee. This is my effort:



            Bitmap greyBitmap = Bitmap.CreateBitmap(1000, 500, Bitmap.Config.Argb8888);
Bitmap mutableBitmap = greyBitmap.Copy(Bitmap.Config.Argb8888, true);//<–true makes copy mutable
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
paint.Color = new Color(Color.ParseColor("#eeeeee"));
canvas.DrawRect(1000, 500, 1000, 500, paint);
vh.dr = new BitmapDrawable(mutableBitmap);


Chances are, I am taking too many steps... Feel free to correct me on this.










share|improve this question




















  • 1





    The coordinates you have in DrawRect() are off. The first two should be 0, 0. Anyway, there's a much simpler way to do that. Just call eraseColor() with your color value on the mutable Bitmap.

    – Mike M.
    Nov 23 '18 at 10:47













  • oh yesm thanks that did the trick. however, would you mind posting both remarks as an answer that i could accept? thank you!

    – Innomotion Media
    Nov 23 '18 at 10:51






  • 1





    Oh, I'm good. Just a couple simple suggestions. :-) Please feel free to post your own answer. This would be a handy duplicate target in the future for users asking specifically for C# code. Btw, I just double checked, and that particular Bitmap.CreateBitmap() overload returns a mutable Bitmap already, so there's no need to copy it first. You can do the whole thing in two lines. Anyhoo, thanks for the offer. I appreciate it. Glad you got it working. Cheers!

    – Mike M.
    Nov 23 '18 at 11:14











  • your wish shall be my command ;)

    – Innomotion Media
    Nov 23 '18 at 11:18














1












1








1








I want to create a bitmap that is filled with only one color (#eeeeee).
I managed to create a bitmap and set it to its viewholder but I cannot seem to find the right way to fill it entierely with my desired color. The output is always only a white rectangle. But it needs to be greyish as in #eeeeee. This is my effort:



            Bitmap greyBitmap = Bitmap.CreateBitmap(1000, 500, Bitmap.Config.Argb8888);
Bitmap mutableBitmap = greyBitmap.Copy(Bitmap.Config.Argb8888, true);//<–true makes copy mutable
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
paint.Color = new Color(Color.ParseColor("#eeeeee"));
canvas.DrawRect(1000, 500, 1000, 500, paint);
vh.dr = new BitmapDrawable(mutableBitmap);


Chances are, I am taking too many steps... Feel free to correct me on this.










share|improve this question
















I want to create a bitmap that is filled with only one color (#eeeeee).
I managed to create a bitmap and set it to its viewholder but I cannot seem to find the right way to fill it entierely with my desired color. The output is always only a white rectangle. But it needs to be greyish as in #eeeeee. This is my effort:



            Bitmap greyBitmap = Bitmap.CreateBitmap(1000, 500, Bitmap.Config.Argb8888);
Bitmap mutableBitmap = greyBitmap.Copy(Bitmap.Config.Argb8888, true);//<–true makes copy mutable
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
paint.Color = new Color(Color.ParseColor("#eeeeee"));
canvas.DrawRect(1000, 500, 1000, 500, paint);
vh.dr = new BitmapDrawable(mutableBitmap);


Chances are, I am taking too many steps... Feel free to correct me on this.







c# android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 10:42









Aniruddh Parihar

2,18911027




2,18911027










asked Nov 23 '18 at 10:41









Innomotion MediaInnomotion Media

266




266








  • 1





    The coordinates you have in DrawRect() are off. The first two should be 0, 0. Anyway, there's a much simpler way to do that. Just call eraseColor() with your color value on the mutable Bitmap.

    – Mike M.
    Nov 23 '18 at 10:47













  • oh yesm thanks that did the trick. however, would you mind posting both remarks as an answer that i could accept? thank you!

    – Innomotion Media
    Nov 23 '18 at 10:51






  • 1





    Oh, I'm good. Just a couple simple suggestions. :-) Please feel free to post your own answer. This would be a handy duplicate target in the future for users asking specifically for C# code. Btw, I just double checked, and that particular Bitmap.CreateBitmap() overload returns a mutable Bitmap already, so there's no need to copy it first. You can do the whole thing in two lines. Anyhoo, thanks for the offer. I appreciate it. Glad you got it working. Cheers!

    – Mike M.
    Nov 23 '18 at 11:14











  • your wish shall be my command ;)

    – Innomotion Media
    Nov 23 '18 at 11:18














  • 1





    The coordinates you have in DrawRect() are off. The first two should be 0, 0. Anyway, there's a much simpler way to do that. Just call eraseColor() with your color value on the mutable Bitmap.

    – Mike M.
    Nov 23 '18 at 10:47













  • oh yesm thanks that did the trick. however, would you mind posting both remarks as an answer that i could accept? thank you!

    – Innomotion Media
    Nov 23 '18 at 10:51






  • 1





    Oh, I'm good. Just a couple simple suggestions. :-) Please feel free to post your own answer. This would be a handy duplicate target in the future for users asking specifically for C# code. Btw, I just double checked, and that particular Bitmap.CreateBitmap() overload returns a mutable Bitmap already, so there's no need to copy it first. You can do the whole thing in two lines. Anyhoo, thanks for the offer. I appreciate it. Glad you got it working. Cheers!

    – Mike M.
    Nov 23 '18 at 11:14











  • your wish shall be my command ;)

    – Innomotion Media
    Nov 23 '18 at 11:18








1




1





The coordinates you have in DrawRect() are off. The first two should be 0, 0. Anyway, there's a much simpler way to do that. Just call eraseColor() with your color value on the mutable Bitmap.

– Mike M.
Nov 23 '18 at 10:47







The coordinates you have in DrawRect() are off. The first two should be 0, 0. Anyway, there's a much simpler way to do that. Just call eraseColor() with your color value on the mutable Bitmap.

– Mike M.
Nov 23 '18 at 10:47















oh yesm thanks that did the trick. however, would you mind posting both remarks as an answer that i could accept? thank you!

– Innomotion Media
Nov 23 '18 at 10:51





oh yesm thanks that did the trick. however, would you mind posting both remarks as an answer that i could accept? thank you!

– Innomotion Media
Nov 23 '18 at 10:51




1




1





Oh, I'm good. Just a couple simple suggestions. :-) Please feel free to post your own answer. This would be a handy duplicate target in the future for users asking specifically for C# code. Btw, I just double checked, and that particular Bitmap.CreateBitmap() overload returns a mutable Bitmap already, so there's no need to copy it first. You can do the whole thing in two lines. Anyhoo, thanks for the offer. I appreciate it. Glad you got it working. Cheers!

– Mike M.
Nov 23 '18 at 11:14





Oh, I'm good. Just a couple simple suggestions. :-) Please feel free to post your own answer. This would be a handy duplicate target in the future for users asking specifically for C# code. Btw, I just double checked, and that particular Bitmap.CreateBitmap() overload returns a mutable Bitmap already, so there's no need to copy it first. You can do the whole thing in two lines. Anyhoo, thanks for the offer. I appreciate it. Glad you got it working. Cheers!

– Mike M.
Nov 23 '18 at 11:14













your wish shall be my command ;)

– Innomotion Media
Nov 23 '18 at 11:18





your wish shall be my command ;)

– Innomotion Media
Nov 23 '18 at 11:18












1 Answer
1






active

oldest

votes


















0














With the fantastic help of Mike M.



        Bitmap greyBitmap = Bitmap.CreateBitmap(1000, 500, Bitmap.Config.Argb8888);
Canvas canvas = new Canvas(greyBitmap );
Paint paint = new Paint();
paint.Color = new Color(Color.ParseColor("#eeeeee")); // set any color here
canvas.DrawRect(0, 0, 1000, 500, paint);
vh.dr = new BitmapDrawable(greyBitmap );





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445113%2fc-android-create-bitmap-with-single-color%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    With the fantastic help of Mike M.



            Bitmap greyBitmap = Bitmap.CreateBitmap(1000, 500, Bitmap.Config.Argb8888);
    Canvas canvas = new Canvas(greyBitmap );
    Paint paint = new Paint();
    paint.Color = new Color(Color.ParseColor("#eeeeee")); // set any color here
    canvas.DrawRect(0, 0, 1000, 500, paint);
    vh.dr = new BitmapDrawable(greyBitmap );





    share|improve this answer




























      0














      With the fantastic help of Mike M.



              Bitmap greyBitmap = Bitmap.CreateBitmap(1000, 500, Bitmap.Config.Argb8888);
      Canvas canvas = new Canvas(greyBitmap );
      Paint paint = new Paint();
      paint.Color = new Color(Color.ParseColor("#eeeeee")); // set any color here
      canvas.DrawRect(0, 0, 1000, 500, paint);
      vh.dr = new BitmapDrawable(greyBitmap );





      share|improve this answer


























        0












        0








        0







        With the fantastic help of Mike M.



                Bitmap greyBitmap = Bitmap.CreateBitmap(1000, 500, Bitmap.Config.Argb8888);
        Canvas canvas = new Canvas(greyBitmap );
        Paint paint = new Paint();
        paint.Color = new Color(Color.ParseColor("#eeeeee")); // set any color here
        canvas.DrawRect(0, 0, 1000, 500, paint);
        vh.dr = new BitmapDrawable(greyBitmap );





        share|improve this answer













        With the fantastic help of Mike M.



                Bitmap greyBitmap = Bitmap.CreateBitmap(1000, 500, Bitmap.Config.Argb8888);
        Canvas canvas = new Canvas(greyBitmap );
        Paint paint = new Paint();
        paint.Color = new Color(Color.ParseColor("#eeeeee")); // set any color here
        canvas.DrawRect(0, 0, 1000, 500, paint);
        vh.dr = new BitmapDrawable(greyBitmap );






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 11:19









        Innomotion MediaInnomotion Media

        266




        266
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445113%2fc-android-create-bitmap-with-single-color%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Fotorealismo