none
[VB2005]請問如何做到capture的效果? RRS feed

  • 問題

  •  

    我寫了個程式, 但很多時需要把程式的畫面capture出來, 但是要經常使用print screen再加小畫家...十分麻煩...

    所以希望各位老大可以教一下小弟加個capture功能...按一下便會自動把目前畫面儲存...並詢問儲存位置...

     

    謝謝

    2007年9月30日 下午 03:14

解答

  • 以下程式片段,可將目前所在的 Form 擷取圖片,並詢問儲存位置。

    程式碼區塊

    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Windows.Forms;

     

    private void button1_Click(object sender, EventArgs e)
    {
        Bitmap screenshot = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
        Graphics graph = Graphics.FromImage(screenshot);

     

        graph.CopyFromScreen(Location.X, Location.Y, 0, 0, Size, CopyPixelOperation.SourceCopy);

     

        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Image File (*.png) | *.png";

        if (sfd.ShowDialog() == DialogResult.OK)
        {
            string filename = sfd.FileName;
            screenshot.Save(filename);
        }
    }

     

     

    2007年9月30日 下午 03:46
  •  

    你的是C#的...我把他改成VB2005的...

     

    程式碼區塊

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim screenshot As New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
        Dim graph As Graphics = Graphics.FromImage(screenshot)
        
        graph.CopyFromScreen(Location.X, Location.Y, 0, 0, Size, CopyPixelOperation.SourceCopy)
        
        Dim sfd As New SaveFileDialog()
        sfd.Filter = "Image File (*.png) | *.png"
        
        If sfd.ShowDialog() = DialogResult.OK Then
            Dim filename As String = sfd.FileName
            screenshot.Save(filename)
        End If
    End Sub

     

     

    但是PixelFormat還沒有宣告...程式不能運行...
    2007年9月30日 下午 04:15
  • 都說你沒有註冊事件了!!!

     

    程式碼區塊

    Imports System.Drawing.Imaging

     

    Public Class Form1
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim screenshot As New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
            Dim graph As Graphics = Graphics.FromImage(screenshot)

            graph.CopyFromScreen(Location.X, Location.Y, 0, 0, Size, CopyPixelOperation.SourceCopy)

            Dim sfd As New SaveFileDialog()
            sfd.Filter = "Image File (*.png) | *.png"

            If sfd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Dim filename As String = sfd.FileName
                screenshot.Save(filename)
            End If
        End Sub
    End Class

     

     

     

    2007年9月30日 下午 05:04

所有回覆

  • 以下程式片段,可將目前所在的 Form 擷取圖片,並詢問儲存位置。

    程式碼區塊

    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Windows.Forms;

     

    private void button1_Click(object sender, EventArgs e)
    {
        Bitmap screenshot = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
        Graphics graph = Graphics.FromImage(screenshot);

     

        graph.CopyFromScreen(Location.X, Location.Y, 0, 0, Size, CopyPixelOperation.SourceCopy);

     

        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Image File (*.png) | *.png";

        if (sfd.ShowDialog() == DialogResult.OK)
        {
            string filename = sfd.FileName;
            screenshot.Save(filename);
        }
    }

     

     

    2007年9月30日 下午 03:46
  •  

    你的是C#的...我把他改成VB2005的...

     

    程式碼區塊

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim screenshot As New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
        Dim graph As Graphics = Graphics.FromImage(screenshot)
        
        graph.CopyFromScreen(Location.X, Location.Y, 0, 0, Size, CopyPixelOperation.SourceCopy)
        
        Dim sfd As New SaveFileDialog()
        sfd.Filter = "Image File (*.png) | *.png"
        
        If sfd.ShowDialog() = DialogResult.OK Then
            Dim filename As String = sfd.FileName
            screenshot.Save(filename)
        End If
    End Sub

     

     

    但是PixelFormat還沒有宣告...程式不能運行...
    2007年9月30日 下午 04:15
  • PixelFormat 為內建類別,搜尋線上手冊就知道他的命名空間在哪裡。

    2007年9月30日 下午 04:18
    版主
  •  chhuang 寫信:

    以下程式片段,可將目前所在的 Form 擷取圖片,並詢問儲存位置。

    程式碼區塊

    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Windows.Forms;

     

    private void button1_Click(object sender, EventArgs e)
    {
        Bitmap screenshot = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
        Graphics graph = Graphics.FromImage(screenshot);

     

        graph.CopyFromScreen(Location.X, Location.Y, 0, 0, Size, CopyPixelOperation.SourceCopy);

     

        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Image File (*.png) | *.png";

        if (sfd.ShowDialog() == DialogResult.OK)
        {
            string filename = sfd.FileName;
            screenshot.Save(filename);
        }
    }

     

     

    謝謝老大..

     

    但是沒有效果...

     

    程式碼區塊

    Imports System
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Windows.Forms

    Public Class Form1

        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim screenshot As New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
            Dim graph As Graphics = Graphics.FromImage(screenshot)

            graph.CopyFromScreen(Location.X, Location.Y, 0, 0, Size, CopyPixelOperation.SourceCopy)

            Dim sfd As New SaveFileDialog()
            sfd.Filter = "Image File (*.png) | *.png"

            If sfd.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim filename As String = sfd.FileName
                screenshot.Save(filename)
            End If
        End Sub
    End Class

     

     

    2007年9月30日 下午 04:44
  •  

    你應該是忘記註冊事件吧....= =

     

    2007年9月30日 下午 04:52
  • 老大可否幫忙寫個可運行的...

     

    c#也不打緊...我打他改回vb的...

     

    辛苦老大了

    2007年9月30日 下午 04:55
  • 都說你沒有註冊事件了!!!

     

    程式碼區塊

    Imports System.Drawing.Imaging

     

    Public Class Form1
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim screenshot As New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
            Dim graph As Graphics = Graphics.FromImage(screenshot)

            graph.CopyFromScreen(Location.X, Location.Y, 0, 0, Size, CopyPixelOperation.SourceCopy)

            Dim sfd As New SaveFileDialog()
            sfd.Filter = "Image File (*.png) | *.png"

            If sfd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Dim filename As String = sfd.FileName
                screenshot.Save(filename)
            End If
        End Sub
    End Class

     

     

     

    2007年9月30日 下午 05:04
  • 謝謝老大...

     

    成功了^^

     

    2007年9月30日 下午 05:25