Panel als .png abspeichern/laden

  • VB.NET

Es gibt 21 Antworten in diesem Thema. Der letzte Beitrag () ist von jvbsl.

    Panel als .png abspeichern/laden

    Hallo ich hab ma ne frage also ihc möchte für ein spiel einen skin editor machen da wollte ich fragen wie kann ich machen das man eine .png datei in einem panel laden kann und wie ich das gezeichnete in einem panel als .png speichern kann?

    danke für jede hilfreiche antwort
    warum in ein Panel? mach es doch einfach in einer PictureBox und zeichnest dann auf das Image mit GDI+, anschließend kannst du das Bild auch über Image.Save abspeichern...
    Ich wollte auch mal ne total überflüssige Signatur:
    ---Leer---
    ich habs ma mit picturebox gemacht nur beim speichern wenn ich

    VB.NET-Quellcode

    1. Private Sub SpeichernToolStripMenuItem_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles SpeichernToolStripMenuItem.Click
    2. PictureBox1.Image.Save("C:\Eigener TW Skin.png", Imaging.ImageFormat.Png)
    3. End Sub

    als code zum speichern doch beim benutzen kommt der fehler
    "System.Runtime.InteropServices.ExternalException wurde nicht behandelt.
    ErrorCode=-2147467259
    Message=Allgemeiner Fehler in GDI+.
    Source=System.Drawing
    StackTrace:
    bei System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
    bei System.Drawing.Image.Save(String filename, ImageFormat format)
    bei Teeworlds_Skin_Creator.Form2.SpeichernToolStripMenuItem_Click1(Object sender, EventArgs e) in C:\Users\Dominik\Desktop\VBSamples\VB Samples\TabletPC Samples\DrawingAttributes\DrawingAttributes\Form2.vb:Zeile 138.
    bei System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
    bei System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
    bei System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
    bei System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
    bei System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
    bei System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
    bei System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
    bei System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
    bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    bei System.Windows.Forms.Control.WndProc(Message& m)
    bei System.Windows.Forms.ScrollableControl.WndProc(Message& m)
    bei System.Windows.Forms.ToolStrip.WndProc(Message& m)
    bei System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
    bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    bei System.Windows.Forms.Application.Run(ApplicationContext context)
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    bei Teeworlds_Skin_Creator.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:Zeile 81.
    bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    bei System.Threading.ThreadHelper.ThreadStart()
    InnerException: "
    wie bekomm ich den weg
    Ich hoffe mal dein Problem gelöst zu haben:

    VB.NET-Quellcode

    1. Dim bmp As New Bitmap(Panel1.Width, Panel1.Height)
    2. Panel1.DrawToBitmap(bmp, New Rectangle(0, 0, Panel1.Width, Panel1.Height))
    3. bmp.Save("Pfad.png", System.Drawing.Imaging.ImageFormat.Png)


    Habe nichts alles durgelessen was geschreiben wurde aber mit diesem Code kanst du den Inhalt deines Panles als png speichern!
    EDIT:einfach Panel1 durch Picturebox ersetzen so geht es auch mit dem
    MFG

    mcandy007 schrieb:

    Ich hoffe mal dein Problem gelöst zu haben:

    VB.NET-Quellcode

    1. Dim bmp As New Bitmap(Panel1.Width, Panel1.Height)
    2. Panel1.DrawToBitmap(bmp, New Rectangle(0, 0, Panel1.Width, Panel1.Height))
    3. bmp.Save("Pfad.png", System.Drawing.Imaging.ImageFormat.Png)


    Habe nichts alles durgelessen was geschreiben wurde aber mit diesem Code kanst du den Inhalt deines Panles als png speichern!
    EDIT:einfach Panel1 durch Picturebox ersetzen so geht es auch mit dem
    MFG
    es kommz dann bei der letzten zeile des codes "Allgemeiner Fehler in GDI+"

    mcandy007 schrieb:

    Darf ich bitte deinen Code mit Pfad sehen?

    Du must auch in das Panel Elemte ziehen wie button und labels!

    MFG

    ich benutze kein panel ich benutz picturebox

    VB.NET-Quellcode

    1. ' Copyright © Microsoft Corporation. All Rights Reserved.
    2. ' This code released under the terms of the
    3. ' Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
    4. '
    5. Imports Microsoft.Ink
    6. Public Class Form2
    7. Dim myInkOverlay As InkOverlay
    8. Sub New()
    9. ' This call is required by the Windows Form Designer.
    10. InitializeComponent()
    11. 'Place the various raster operations in the group box
    12. InitializeRasterOperations()
    13. End Sub
    14. 'Handle the form loading event by wiring up ink collection
    15. Sub FormLoadHandler(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    16. 'Create and enable the ink collecting Control
    17. myInkOverlay = New InkOverlay(PictureBox2)
    18. myInkOverlay.Enabled = True
    19. 'Set the attributes of the ink collector
    20. SetAttributes()
    21. End Sub
    22. 'Creates controls for selecting raster operation
    23. Sub InitializeRasterOperations()
    24. 'Add all the types of Raster Operations to the groupbox for possible selection
    25. For Each rasterOpName As String In System.Enum.GetNames(GetType(RasterOperation))
    26. 'Create a new radio button
    27. Dim rasterButton As New RadioButton()
    28. rasterButton.Text = rasterOpName
    29. 'Default raster op is "Copy Pen"
    30. If rasterOpName = "CopyPen" Then
    31. rasterButton.Checked = True
    32. End If
    33. 'Add it to the flow layout panel
    34. FlowLayoutPanel1.Controls.Add(rasterButton)
    35. 'Add a handler to it
    36. AddHandler rasterButton.CheckedChanged, AddressOf RasterOpChosenHandler
    37. Next
    38. End Sub
    39. 'Handles selections of raster operation
    40. Sub RasterOpChosenHandler(ByVal sender As Object, ByVal e As EventArgs)
    41. 'Who has been chosen?
    42. Dim chosenButton As RadioButton = CType(sender, RadioButton)
    43. 'This will be called if checked or unchecked, so only assign on checked
    44. If chosenButton.Checked Then
    45. 'What's the RasterOperation value of selected button?
    46. Dim rasterOp = System.Enum.Parse(GetType(RasterOperation), chosenButton.Text)
    47. 'Set attribute
    48. myInkOverlay.DefaultDrawingAttributes.RasterOperation = rasterOp
    49. End If
    50. End Sub
    51. Sub SetAttributesHandler(ByVal sender As Object, ByVal e As EventArgs) Handles antiAliasCheckbox.CheckedChanged, pressureSensitiveCheckbox.CheckedChanged, penTipRectangle.CheckedChanged, penTipEllipse.CheckedChanged, widthUpDown.ValueChanged, transparencyUpDown.ValueChanged, heightUpDown.ValueChanged
    52. 'Can be called prior to myInkOverlay being initialized, so check if that's the case
    53. If myInkOverlay Is Nothing Then
    54. Return
    55. End If
    56. SetAttributes()
    57. End Sub
    58. 'Sets the major attributes of the ink overlay (minus color, raster op, and some rare properties
    59. Sub SetAttributes()
    60. 'Anti-aliasing
    61. If antiAliasCheckbox.Checked = True Then
    62. myInkOverlay.DefaultDrawingAttributes.AntiAliased = True
    63. Else
    64. myInkOverlay.DefaultDrawingAttributes.AntiAliased = False
    65. End If
    66. 'Pressure sensitivity
    67. If pressureSensitiveCheckbox.Checked = True Then
    68. myInkOverlay.DefaultDrawingAttributes.IgnorePressure = False
    69. Else
    70. myInkOverlay.DefaultDrawingAttributes.IgnorePressure = True
    71. End If
    72. 'Pen Tip
    73. If penTipRectangle.Checked = True Then
    74. myInkOverlay.DefaultDrawingAttributes.PenTip = PenTip.Rectangle
    75. Else
    76. myInkOverlay.DefaultDrawingAttributes.PenTip = PenTip.Ball
    77. End If
    78. 'Transparency
    79. myInkOverlay.DefaultDrawingAttributes.Transparency = transparencyUpDown.Value
    80. 'Height
    81. myInkOverlay.DefaultDrawingAttributes.Height = heightUpDown.Value
    82. 'Width
    83. myInkOverlay.DefaultDrawingAttributes.Width = widthUpDown.Value
    84. End Sub
    85. 'Handles "color" button
    86. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    87. 'Create a standard dialog
    88. Dim colorDialog As New ColorDialog()
    89. 'Set color to current color
    90. colorDialog.Color = myInkOverlay.DefaultDrawingAttributes.Color
    91. 'Show it
    92. If colorDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
    93. 'Set color to chosen
    94. myInkOverlay.DefaultDrawingAttributes.Color = colorDialog.Color
    95. End If
    96. End Sub
    97. Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)
    98. End Sub
    99. Private Sub BeendenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BeendenToolStripMenuItem.Click
    100. Me.Close()
    101. SplashScreen1.Close()
    102. End Sub
    103. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    104. AboutBox1.Show()
    105. End Sub
    106. Private Sub SpeichernToolStripMenuItem_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles SpeichernToolStripMenuItem.Click
    107. Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
    108. PictureBox1.DrawToBitmap(bmp, New Rectangle(0, 0, PictureBox1.Width, PictureBox1.Height))
    109. bmp.Save("C:\Eigener TW Skin.png", System.Drawing.Imaging.ImageFormat.Png)
    110. End Sub
    111. Private Sub LoadToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadToolStripMenuItem.Click
    112. End Sub
    113. End Class


    microsoft code ding is nur wg sample
    @mcandy007:in diesem fall äußerst unschön, er will linien zeichnen...

    @Threadersteller: wahrscheinlich Win Vista + und wenn du da auf C speicherst haste wohl kaum Zugriff...
    Ich wollte auch mal ne total überflüssige Signatur:
    ---Leer---
    Komisch bei mir geht:

    VB.NET-Quellcode

    1. Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
    2. PictureBox1.DrawToBitmap(bmp, New Rectangle(0, 0, PictureBox1.Width, PictureBox1.Height))
    3. bmp.Save("Pfad.png", System.Drawing.Imaging.ImageFormat.Png)


    Nimm einen anderen Pfad! Nicht C:\ der benötigt ja die admin rechte unter vista und win 7!

    MFG

    mcandy007 schrieb:

    Komisch bei mir geht:

    VB.NET-Quellcode

    1. Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
    2. PictureBox1.DrawToBitmap(bmp, New Rectangle(0, 0, PictureBox1.Width, PictureBox1.Height))
    3. bmp.Save("Pfad.png", System.Drawing.Imaging.ImageFormat.Png)


    Nimm einen anderen Pfad! Nicht C:\ der benötigt ja die admin rechte unter vista und win 7!

    MFG

    wie gesagt ich hab alle 250gb in eine partition gesteckt

    Oder soll ich beim dl auf meiner seite schreiben nur mit win xp kompatibel(Das ist uhralt)
    Wir meinen das der Pfad so ca. lauten muss:

    C:\Users\mcandy007\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\bin\Debug\bild.png

    Aber nicht: C:\bild.png

    Oder vorher vb als Admin starten

    MFG ;)

    mcandy007 schrieb:

    Mach einen Safefile dialog rein! Dann kanst mehrere pfäde schneller probieren.
    MFG
    edit klarstellen is gemalt in picture
    ich hab safefile noch nie genutzt kann mir da jemand einen code geben

    Das mit Speichern in Bilder geht aber wenn ich speichere kommtnur der hintergrund der picturebox >

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Hydropie“ ()

    Den Safefiledialog auf Form ziehen und beim Button den Code:

    VB.NET-Quellcode

    1. SaveFileDialog1.Filter = "PNG-Bild|*.png"
    2. If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    3. Dim pfad As String
    4. pfad = SaveFileDialog1.FileName
    5. Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
    6. PictureBox1.DrawToBitmap(bmp, New Rectangle(0, 0, PictureBox1.Width, PictureBox1.Height))
    7. bmp.Save(pfad, System.Drawing.Imaging.ImageFormat.Png)
    8. End If

    MFG

    EDIT dann versuchs doch mit einem Panel! und ziehe aber die Picbox in das Panel rein!

    Vieleicht geht das!