Solved

GDI DrawImage Scale problem

Posted on 2006-07-04
7
2,868 Views
Last Modified: 2011-10-03
Hi Experts,
Any help on this is greatly appreciated...
I wrote a little test with a 40X40 bitmap.  I filled it with a checkerboard pattern.  If I scale this bitmap by a zoomfactor I always get a first row and column that is half the size it is supposed to be.
Example:  zoomed to 80x80, all squares on the checkerboard pattern are 2x2 except the first column and row which are 1X2. This happens for all zoom levels.

Paint routine for user control:
 Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Try
            If (bmp Is Nothing) Then
            Else
                Dim g As Graphics = e.Graphics()
                g.SmoothingMode = Drawing2D.SmoothingMode.None
                g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
                g.SmoothingMode = Drawing2D.SmoothingMode.None

             
                Dim rect As New Rectangle
                rect.X = 0
                rect.Y = 0
                rect.Width = (bmp.Width * m_ZoomLevel)
                rect.Height = (bmp.Height * m_ZoomLevel)

                'attribute shows that destination rect is the right size but not needed for test.
                Dim attr As System.Drawing.Imaging.ImageAttributes
                attr = New System.Drawing.Imaging.ImageAttributes()
                attr.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY)

                g.DrawImage(bmp, rect, 0, 0, 40, 40, GraphicsUnit.Pixel, attr)
                attr.Dispose()
            End If

     

        Catch ex As Exception
            MessageBox.Show("ouch")
        End Try
    End Sub

Thanks again for any help.  I' ll give 500 points for this one.
0
Comment
Question by:Jas001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
7 Comments
 
LVL 27

Expert Comment

by:planocz
ID: 17047883
Hi  Jas001,

Here is some code that I had for zooming and resizing. It might help in your search for and answer.


'FORM 1

Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Public Class frmZoomImage
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents PictureBox3 As System.Windows.Forms.PictureBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmZoomImage))
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.PictureBox2 = New System.Windows.Forms.PictureBox
        Me.Button2 = New System.Windows.Forms.Button
        Me.PictureBox3 = New System.Windows.Forms.PictureBox
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
        Me.PictureBox1.Location = New System.Drawing.Point(212, 24)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(16, 16)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(12, 52)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Resize"
        '
        'PictureBox2
        '
        Me.PictureBox2.Location = New System.Drawing.Point(116, 24)
        Me.PictureBox2.Name = "PictureBox2"
        Me.PictureBox2.Size = New System.Drawing.Size(32, 32)
        Me.PictureBox2.TabIndex = 2
        Me.PictureBox2.TabStop = False
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(8, 156)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "Zoom"
        '
        'PictureBox3
        '
        Me.PictureBox3.Location = New System.Drawing.Point(96, 128)
        Me.PictureBox3.Name = "PictureBox3"
        Me.PictureBox3.Size = New System.Drawing.Size(508, 328)
        Me.PictureBox3.TabIndex = 4
        Me.PictureBox3.TabStop = False
        '
        'frmZoomImage
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(644, 482)
        Me.Controls.Add(Me.PictureBox3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.PictureBox2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox1)
        Me.Name = "frmZoomImage"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private Sub ZoomImage(ByVal pictSource As PictureBox, ByVal pictDestination As PictureBox, ByVal rectSource As Rectangle, Optional ByVal antiAlias As Boolean = False)
        Dim w As Integer = pictDestination.Width
        Dim h As Integer = pictDestination.Height

        pictDestination.SizeMode = PictureBoxSizeMode.Normal
        ' Get the source Bitmap.
        Dim bmpSource As Bitmap = New Bitmap(pictSource.Image)
        ' Make the destination Bitmap.
        Dim bmpDestination As Bitmap = New Bitmap(w, h)
        ' Copy the image.
        Dim gr As Graphics = Graphics.FromImage(bmpDestination)
        If antiAlias Then
            gr.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
        End If

        Dim rectDestination As Rectangle = New Rectangle(0, 0, w, h)
        gr.DrawImage(bmpSource, rectDestination, rectSource, GraphicsUnit.Pixel)
        ' Display the result.
        pictDestination.Image = bmpDestination
        pictDestination.SizeMode = PictureBoxSizeMode.StretchImage
    End Sub 'ZoomImage'
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.PictureBox2.Image = FixedSize(Me.PictureBox1.Image, Me.PictureBox2.Width, Me.PictureBox2.Height)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ZoomImage(Me.PictureBox1, Me.PictureBox3, New Rectangle(0, 0, Me.PictureBox1.Width \ 2, Me.PictureBox1.Height \ 2))
    End Sub
    Private Function FixedSize(ByVal imgPhoto As Image, ByVal Width As Integer, ByVal Height As Integer) As Image
        Dim sourceWidth As Integer = imgPhoto.Width
        Dim sourceHeight As Integer = imgPhoto.Height
        Dim sourceX As Integer = 0
        Dim sourceY As Integer = 0
        Dim destX As Integer = 0
        Dim destY As Integer = 0

        Dim nPercent As Single = 0
        Dim nPercentW As Single = 0
        Dim nPercentH As Single = 0

        nPercentW = (CType(Width / CType(sourceWidth, Double), Double))
        nPercentH = (CType(Height / CType(sourceHeight, Double), Double))
        If nPercentH < nPercentW Then
            nPercent = nPercentH
            destX = System.Convert.ToInt16((Width - (sourceWidth * nPercent)) / 2)
        Else
            nPercent = nPercentW
            destY = System.Convert.ToInt16((Height - (sourceHeight * nPercent)) / 2)
        End If

        Dim destWidth As Integer = CType((sourceWidth * nPercent), Integer)
        Dim destHeight As Integer = CType((sourceHeight * nPercent), Integer)
        Dim bmPhoto As Bitmap
        bmPhoto = New Bitmap(Width, Height, PixelFormat.Format24bppRgb)
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)

        Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
        grPhoto.Clear(Color.White)
        grPhoto.InterpolationMode = InterpolationMode.NearestNeighbor

        grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel)

        grPhoto.Dispose()
        Return bmPhoto
    End Function
End Class
0
 
LVL 2

Author Comment

by:Jas001
ID: 17052219
Thanks for the post but nope yours has the same problem.  Just switch the antialias to true and you'll see the same problem.  I've posted a snapshot of the problem on my blog.
<a href="http://codemonkeyjas.blogspot.com/2006/07/problem-stretching-images-with-gdi.html">codemonkeyjas.blogspot.com</a>

I was wondering if maybe the Interpolation method set to nearest neighbor has a bug in it...  I'll try posting on the .net user groups as well, but if someone finds the solution I'll award them the points.
0
 
LVL 2

Author Comment

by:Jas001
ID: 17073800
The bug has been posted to Microsoft and confirmed by others.  See my blog: http://codemonkeyjas.blogspot.com/2006/07/problem-stretching-images-with-gdi.html
for the answer.
0
 
LVL 2

Author Comment

by:Jas001
ID: 17285248
TheLeanedOne,
I'm not quite sure why you recommended no points refunded.
I think you should refund the points since I did post the answer to the problem.
Jas001
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 17306711
PAQ / Refund
ee ai construct, community support moderator
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question