Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7644279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:38:55+00:00 2026-05-31T09:38:55+00:00

I have created a image button for my apps on windows mobile. There is

  • 0

I have created a image button for my apps on windows mobile. There is a small problem though, when i have different screen resolutions,bigger for example, the image isn’t automatic resized to fit in the control. How can this be done?

thanks in advance.

this is my code for the onPaint function

        Dim gxOff As Graphics
        'Offscreen graphics
        Dim imgRect As Rectangle
        'image rectangle
        Dim backBrush As Brush
        'brush for filling a backcolor
        If m_bmpOffscreen Is Nothing Then
            'Bitmap for doublebuffering
            m_bmpOffscreen = New Bitmap(Me.Width, Me.Height)
        End If

        gxOff = Graphics.FromImage(m_bmpOffscreen)

        gxOff.Clear(Me.BackColor)

        If Not bPushed Then
            backBrush = New SolidBrush(Parent.BackColor)
        Else
            backBrush = New SolidBrush(Color.Black)
            'change the background when it's pressed
        End If

        gxOff.FillRectangle(backBrush, Me.ClientRectangle)

        If m_image IsNot Nothing Then
            'Center the image relativelly to the control
            Dim imageLeft As Integer = (Me.Width - m_image.Width) / 2
            Dim imageTop As Integer = (Me.Height - m_image.Height) / 2

            If Not bPushed Then
                imgRect = New Rectangle(imageLeft, imageTop, m_image.Width, m_image.Height)
            Else
                'The button was pressed
                'Shift the image by one pixel
                imgRect = New Rectangle(imageLeft + 1, imageTop + 1, m_image.Width, m_image.Height)
            End If
            'Set transparent key
            Dim imageAttr As New Imaging.ImageAttributes()
            imageAttr.SetColorKey(BackgroundImageColor(m_image), BackgroundImageColor(m_image))
            'Draw image
            gxOff.DrawImage(m_image, imgRect, 0, 0, m_image.Width, m_image.Height, GraphicsUnit.Pixel, imageAttr)
        End If

        If bPushed Then
            'The button was pressed
            'Prepare rectangle
            Dim rc As Rectangle = Me.ClientRectangle
            rc.Width -= 1
            rc.Height -= 1
            'Draw rectangle
            gxOff.DrawRectangle(New Pen(Color.Black), rc)
        End If

        'Draw from the memory bitmap
        e.Graphics.DrawImage(m_bmpOffscreen, 0, 0)

        MyBase.OnPaint(e)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:38:57+00:00Added an answer on May 31, 2026 at 9:38 am

    You need to scale image during OnPaint e.g.:

    if (this._image != null)
    {                    
      int x = (this.Width - this._image.Width) / 2;
      int y = (this.Height - this._image.Height) / 2;
      var imgRect = new Rectangle(x, y, this._image.Width, this._image.Height);
    
      var imageAttr = new ImageAttributes();
    
      Color clr = this._image.GetPixel(0, 0);
      imageAttr.SetColorKey(clr, clr);
    
      gr2.DrawImage(this._image, imgRect, 0, 0, this._image.Width, this._image.Height, GraphicsUnit.Pixel, imageAttr);
    }
    

    Above example is how to draw image with transparent color based on first pixel. Now you need to ammend second 3 lines:

    bool qvga = Screen.PrimaryScreen.Bounds.Height == 240 || Screen.PrimaryScreen.Bounds.Width == 240; 
    int x = (this.Width - (qvga ? this._image.Width : this._image.Width * 2)) / 2;
    int y = (this.Height - (qvga ? this._image.Height : this._image.Height * 2)) / 2;
    var imgRect = new Rectangle(x, y, (qvga ? this._image.Width : this._image.Width * 2), (qvga ? this._image.Height : this._image.Height * 2));
    

    [Edit]

    Seeing your code change is straight forward. Like I described in comment: For two main windows mobile resolutions, QVGA and VGA, we assuming that image is for QVGA size. Please convert my code to VB by yourself. So set qvga flag as I written:

    bool qvga = Screen.PrimaryScreen.Bounds.Height == 240 || Screen.PrimaryScreen.Bounds.Width == 240; 
    

    now based on flag ammend following based on my above code:

    Dim imageLeft As Integer = (Me.Width - m_image.Width) / 2
    Dim imageTop As Integer = (Me.Height - m_image.Height) / 2
    

    and then this ones:

    If Not bPushed Then
         imgRect = New Rectangle(imageLeft, imageTop, m_image.Width, m_image.Height)
    Else
         'The button was pressed
         'Shift the image by one pixel
    imgRect = New Rectangle(imageLeft + 1, imageTop + 1, m_image.Width, m_image.Height)
    

    End If

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a button CSS class that uses background images with different positions
I have created a Windows CE6 Image through Platform Builder, and I now want
I have problem with dynamically created image (JavaScript). I want to change the innerHTML
i have created an image button in my xml <Button android:layout_height=wrap_content android:id=@+id/send android:layout_width=50dip android:background=@drawable/button_back>
hi I am creating firefox plugin.I have created a button image on toolbar in
This way i have created my custom button and set the image.I have set
i have created a iPad application and i have used a share image button
I have created an image map with flash, I have separate button functions that
How can I create in a Swing interface a toggle image button? I have
I have created a XML image gallery, which displays text in between each slide.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.