• 0

[vb .net] Resize Image


Question

I have an image declared as

dim backImage as image

I then apply this image to the background of a form. However when the form resize's I wan tto resize the image and then re-apply it. How can i resize the image? the size property (bakImage.size) is readonly.

Link to comment
https://www.neowin.net/forum/topic/85732-vb-net-resize-image/
Share on other sites

17 answers to this question

Recommended Posts

  • 0

You will have to recreate the image to the new size and then paint it back on.

Dim imgBuffer As New Bitmap(Me.Size)
imgBuffer.CreateGraphics.DrawImage(backImage, 0, 0, Me.Size)
Me.BackgroundImage = imgBuffer
imgBuffer.Dispose

(Excuse errors, this is from the top of my head.)

  • 0

if it needs fixing, I have no idea what to fix because first off I'm not using bitmaps although maybe with new bitmap i don't have to. Also there is no such thing under system.drawing for creategraphics and it also says no image accepts that many arguments.

  • 0

i am trying ot use this for the resize event, but the form just disappears then, but i think this is the right track:

       backImage = New System.Drawing.Bitmap(Me.Width, Me.Height).FromFile("Skins\" & skinChoice & "\background.jpg"
        backImage = Bitmap.FromFile("Skins\" & skinChoice & "\background.jpg")
        Me.BackgroundImage = backImage

  • 0

Try this for the paint and resize events (C# of course):

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
	Graphics g = e.Graphics;
	Bitmap bmp = new Bitmap("Lightening.jpg");
	Rectangle r = new Rectangle(0,0,bmp.Width,bmp.Height);
	g.DrawImage(bmp,this.ClientRectangle);
}

private void Form1_Resize(object sender, System.EventArgs e)
{
	this.Invalidate();
}

  • 0

You're probably going to get a lot of flicker. Forms, and I believe most controls don't support double buffering by default.

I made an example project a bit ago that demonstrated how to resize a bitmap by subclassing a panel control and making it double buffer by using the SetStyle method of the control.

Public Class UserPanel
 ?Inherits System.Windows.Forms.Panel

#Region " Windows Form Designer generated code "

 ? ?Public Sub New()
 ? ? ? ?MyBase.New()

 ? ? ? ?'This call is required by the Windows Form Designer.
 ? ? ? ?InitializeComponent()
 ? ?Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or _
 ? ? ? ? ControlStyles.UserPaint Or _
 ? ? ? ?ControlStyles.DoubleBuffer, True)

 ? ? ? ?'Add any initialization after the InitializeComponent() call

 ? ?End Sub

 ? ?'UserControl 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.
 ? ?<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
 ? ? ? ?components = New System.ComponentModel.Container()
 ? ?End Sub

#End Region

End Class

Then, use the code that noll presented, except handle the panel's paint and resize events, and you'll probably want to make your image a class variable instead of creating it each time the form paints.

? ? ? ?img = Image.FromFile("animage.jpg") ' or whatever
 ? ? ? ?panel = New UserPanel
 ? ? ? ?lbl = New Label
 ? ? ? ?'This call is required by the Windows Form Designer.
 ? ? ? ?InitializeComponent()
 ? ? ? ?AddHandler panel.Paint, AddressOf panel_OnPaint
 ? ? ? ?AddHandler panel.Resize, AddressOf panel_OnResize

 ? ? ? ?' add the control to the form
 ? ? ? ?Me.Controls.Add(panel)

 ? ? ? ?'.... other form code

 ? ?Private Sub panel_OnPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
 ? ? ? ?Dim rect As New Rectangle(0, 0, panel.Width, panel.Height)
 ? ? ? ?e.Graphics.DrawImage(img, rect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel)
 ? ?End Sub

 ? ?Private Sub panel_OnResize(ByVal sender As Object, ByVal e As System.EventArgs)
 ? ? ? ?panel.Invalidate()
 ? ?End Sub

  • 0

thanks weenur but the form is very simple and noah's code is perfect for what i wound up doing. i didn't like having a window border around a skinned window, so instead, I have it where the user can override the default skin set size but going into the config panel and entering the new values, a little less functional but works well enough.

  • 0

Actually, it's much easier to use double buffering as it's supported directly by GDI+. To enable it, insert these three lines of code in the load event for the form:

this.SetStyle(ControlStyles.DoubleBuffer,true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
this.SetStyle(ControlStyles.UserPaint,true);

Substitute VB for the C# and you should be flicker free.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • No I don't also get my news for one source. Have you considered that maybe I just don't care about tech enough to spend time reading multiple sources? Anyway my website preferences are way off topic.
    • The bloat on windows after 7 was acceptable, but 11 its even worse than chinese android phone makers, because at least they can remove the ads or apps, but w11 shove you bloat with no option to remove it, it seems that 25h2 will fix that
    • When I say I don't see the appeal, I meant as in general, of the character within the story. I guess apart from being a foil, as mentioned, but I still feel it could probably be done a bit more...less on the nose? A bit more nuanced? Something like that. He really just felt more like a chance to cast Fillion more than anything (to me, anyway). Obviously I am aware that Guy Gardner is not exactly meant to be an *appealing* character
    • This excellent 27-inch 1440p 240Hz QD-OLED gaming monitor is way under $500 by Sayan Sen If you are on the hunt for a great gaming monitor that is under $500, then AOC has a great option today that is worth considering. The model in question is the AOC Q27G4ZD which is a 27-inch 240 Hz display based on QD-OLED (Quantum Dot OLED) and it is at just $428 making it lowest ever priced (purchase link under the specs table below). The specialty of QD-OLED is that pairs the self-emissive OLED pixels with a layer of quantum dots, converting blue OLED light into pure red and green wavelengths more efficiently than traditional color filters. The result is brighter highlights, wider color gamut, and higher color volume without sacrificing OLED’s infinite contrast and instant response. Quantum dots are also said to reduce energy loss in color conversion, while boosting luminance and vibrancy. Thanks to the excellent color reproduction alongside the contrast, this QD-OLED monitor can be used for professional photo/video editing, animations, and such as well, plus working on spreadsheets and other office docs should be a breeze too. The technical specifications of the AOC Q27G4ZD are given below: Specification Details Diagonal Screen Size 26.5 " (67.3 cm) Panel Technology Quantum Dot OLED (QD-OLED) Maximum Refresh Rate 240 Hz (DP 1.4) 144 Hz (HDMI 2.0) Response Time (GtG) 0.03 ms (GtG) Brightness (Typ.) 450 cd/m² (10 % APL) Peak Brightness (HDR) 1000 cd/m² (3 % APL) Color Gamuts RGB 100% (CIE1931) / DCI-P3 99% (CIE1976) Color Accuracy Delta E < 2 Display Colors 1.07Billion HDR Certification VESA DisplayHDR™ True Black 400 Sync Technology Adaptive-Sync Contrast Ratio 1,500,000:1 (Typical) Color Depth 10-bit Connectivity 1 × DisplayPort 1.4 2 × HDMI 2.0 1 × 3.5 mm Audio Out 4x USB 3.2 Gen1 Ergonomics Height: 130 mm Tilt: –5 ° ~ 23 ° Swivel: –30 ° ~ 30 ° Pivot: –90 ° ~ 90 ° Bezel 3-sided frameless Special Features Flicker-Free, Low Blue Mode, Gaming Modes (FPS/Racing/RTS/Gamer1–3), Shadow Control, Game Color, Dial Point, Sniper Scope, Low Input Lag Get the AOC Q27G4ZD 27" QD-OLED at the link below: AOC Q27G4ZD 27" QD OLED Gaming Monitor, 3-Year Zero-Bright-Dot: $427.69 (Sold and Shipped by Amazon US) This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • But even when it says local, it is not local, they all send info back. I bet if you unplug your computer from the net, they would not work Not saying that AI can't do useful stuff, just I don't want it pushed onto me all the flipping time
  • Recent Achievements

    • Week One Done
      MIghty Haul earned a badge
      Week One Done
    • One Month Later
      MIghty Haul earned a badge
      One Month Later
    • Collaborator
      KD2004 earned a badge
      Collaborator
    • One Month Later
      ataho31016 earned a badge
      One Month Later
    • One Month Later
      Delahenty Machinery earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      594
    2. 2
      Michael Scrip
      200
    3. 3
      ATLien_0
      192
    4. 4
      +FloatingFatMan
      140
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!