Drawimage?

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
8 posts Page 1 of 1
Contributors
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Drawimage?
Dummy1912
Hello dear friends,

It drives us nuts :lol:
even ggle didn't help much

so i want to set the image any location we like to add
even if we want it center it shall be center even when you resize the button
but don't work :(
even it change but it don't get into the middle and even if you resize its just devil;

here the code:
Code: Select all
'ImageAlign = middlecenter or any possition you want like the real button control
'topleft , topcenter ect...
'ImageSize = 16,16

Dim ipt As PointF = ImageLocation(GetStringFormat(_ImageAlign), _
                                    Me.Size, _ImageSize)
Code: Select all
Private Shared Function ImageLocation(ByVal sf As StringFormat, ByVal Area As SizeF, ByVal ImageArea As SizeF) As PointF
        Dim pt As PointF
        Select Case sf.Alignment
            Case StringAlignment.Center
                pt.X = CSng((Area.Width - ImageArea.Width) / 3)
            Case StringAlignment.Near
                pt.X = 8
            Case StringAlignment.Far
                pt.X = Area.Width - ImageArea.Width - 8

        End Select

        Select Case sf.LineAlignment
            Case StringAlignment.Center
                pt.Y = CSng((Area.Height - ImageArea.Height) / 3)
            Case StringAlignment.Near
                pt.Y = 8
            Case StringAlignment.Far
                pt.Y = Area.Height - ImageArea.Height - 8

        End Select

        Return pt
    End Function
Code: Select all
 e.Graphics.DrawImage(_Image, ipt.X, ipt.Y, _
                                                    _ImageSize.Width, _ImageSize.Height)
Code: Select all
    Private _ImageAlign As ContentAlignment = ContentAlignment.MiddleLeft

    Public Property ImageAlign() As ContentAlignment
        Get
            Return _ImageAlign
        End Get
        Set(ByVal Value As ContentAlignment)
            _ImageAlign = Value
            Invalidate()
        End Set
    End Property

so like the default button we want also get this result with any align.
Image

hope somebody can help ;)

thanks
Last edited by Dummy1912 on Sat Apr 05, 2014 6:58 am, edited 1 time in total.
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: Drawimage?
Usman55
Isn't it as simple as setting the ImageAlign property to TopCenter or MiddleCenter?

I don't understand what you're trying to achieve.
Image
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Re: Drawimage?
Dummy1912
awww not again :lol:
whats so hard to understand... #Usman55

i'm sure you know the functions of the default button image property
so i want to make the same property for my own button

to set the image topcenter, middlecenter ect.....
but i can't manage to get the correct position
and even when i resize the button the image stays at the same location instead to move so it stays at the topcenter or middlecenter or what align i set.
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: Drawimage?
Usman55
You're creating a custom button control and you want to have the ImageAlign property in it?

I see you have a picturebox within which the location of the image is to be changed.

To make the image move when the button is resized, you need to put the code in Button_SizeChanged event.

I'll look into the calculations when I get back to my computer.
Image
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Re: Drawimage?
Dummy1912
#Usman55
its not a picturebox its a image :)
and yes we want to get the ImageAlign property

i updated my code and added the ImageAlign Property

and thanks :)
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Re: Drawimage?
Dummy1912
isn't anyone that can help me please?? :?
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: Drawimage?
XTechVB
I created a custom control based on how i understood your question/problem. I hope it helps :D (C#) :D
Code: Select all
using System;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsFormsApplication1
{
    class DummyCNS : Control
    {
        #region Constructor
        public DummyCNS()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
        }
        #endregion

        #region Properties
        private Image _ControlImage = null;
        public Image ControlImage 
        {
            get { return _ControlImage; }
            set { _ControlImage = value; this.Invalidate(); }
        }

        private ContentAlignment _ImageAlign = ContentAlignment.MiddleCenter;
        public ContentAlignment ImageAlign
        {
            get { return _ImageAlign; }
            set { _ImageAlign = value; this.Invalidate(); }
        }

        private int _ImageMargin = 5;
        public int ImageMargin
        {
            get { return _ImageMargin; }
            set { _ImageMargin = value; this.Invalidate(); }
        }
        #endregion

        #region Override System Paint
        protected override void OnPaint(PaintEventArgs e)
        {
            if (_ControlImage != null)
            {
                if (_ImageAlign == ContentAlignment.TopLeft)
                {
                    e.Graphics.DrawImageUnscaled(_ControlImage, _ImageMargin, _ImageMargin);
                }
                else if (_ImageAlign == ContentAlignment.TopCenter)
                {
                    e.Graphics.DrawImageUnscaled(_ControlImage, this.Width / 2 - (_ControlImage.Width / 2), _ImageMargin);
                }
                else if (_ImageAlign == ContentAlignment.TopRight)
                {
                    e.Graphics.DrawImageUnscaled(_ControlImage, (this.Width - _ControlImage.Width) - _ImageMargin, _ImageMargin);
                }
                else if (_ImageAlign == ContentAlignment.MiddleLeft)
                {
                    e.Graphics.DrawImageUnscaled(_ControlImage, _ImageMargin, this.Height / 2 - (_ControlImage.Height / 2));
                }
                else if (_ImageAlign == ContentAlignment.MiddleCenter)
                {
                    e.Graphics.DrawImageUnscaled(_ControlImage, this.Width / 2 - (_ControlImage.Width / 2), this.Height / 2 - (_ControlImage.Height / 2));
                }
                else if (_ImageAlign == ContentAlignment.MiddleRight)
                {
                    e.Graphics.DrawImageUnscaled(_ControlImage, (this.Width - _ControlImage.Width) - _ImageMargin, this.Height / 2 - (_ControlImage.Height / 2));
                }
                else if (_ImageAlign == ContentAlignment.BottomLeft)
                {
                    e.Graphics.DrawImageUnscaled(_ControlImage, _ImageMargin, (this.Height - _ControlImage.Height) - _ImageMargin);
                }
                else if (_ImageAlign == ContentAlignment.BottomCenter)
                {
                    e.Graphics.DrawImageUnscaled(_ControlImage, this.Width / 2 - (_ControlImage.Width / 2), (this.Height - _ControlImage.Height) - _ImageMargin);
                }
                else if (_ImageAlign == ContentAlignment.BottomRight)
                {
                    e.Graphics.DrawImageUnscaled(_ControlImage, (this.Width - _ControlImage.Width) - _ImageMargin, (this.Height - _ControlImage.Height) - _ImageMargin);
                }
            }
        }
        #endregion
    }
}
No offense buddy, but you're terrible at explaining things :D :D :lol:
You can find me on Facebook or on Skype mihai_92b
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Re: Drawimage?
Dummy1912
Yea yea yea we know #XTechVB :lol:
and thanks for this example it works great even if we resize the button ;)

+rep loove;
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
8 posts Page 1 of 1
Return to “Coding Help & Support”