创建图形并将其另存为位图

时间:2010-05-21 09:16:04

标签: c# .net bitmap picturebox

我有两个问题:

1)我有PictureBox,其Dock设置为Fill。当我调整Form的大小时,我无法在扩展的PictureBox部分创建图形。有什么问题?

2)我想将PictureBox上创建的图形转换为Bitmap并将其另存为 * .JPG或* .bmp。我怎么能这样做?

4 个答案:

答案 0 :(得分:7)

您可以使用handle device从图片框中取出位图

Graphics g = pictureBox1.CreateGraphics();          
Bitmap bitMap = Bitmap.FromHbitmap(g.GetHdc());
bitMap.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

甚至更好,如果pictureBox没有修改图像,你可以直接从pictureBox控件中获取图像

pictureBox1.Image.Save("path", System.Drawing.Imaging.ImageFormat.Jpeg);

答案 1 :(得分:2)

试试这个,对我来说很好......

//Register receiver for bluetooth discovery
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            //discovery starts, we can show progress dialog or perform other tasks
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            //discovery finishes, dismiss progress dialog
        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            //bluetooth device found for pair
            try {
// Here I want to know device is obd or not?
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                //Make pair of obd device as per name:
                if (device != null && !device.getName().equals("")) {
                    LogUtils.LOGE("NEW DEVICE", device.getName());
                    if (device.getName().equals(OBD_DEVICE_NAME_ONE) ||
                            device.getName().equals(OBD_DEVICE_NAME_TWO) ||
                            device.getName().equals(OBD_DEVICE_NAME_THREE)) {
                        pairDevice(device);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
};

//Send pairing request to OBD Device
private void pairDevice(BluetoothDevice device) {
    try {
        Method method = device.getClass().getMethod("createBond", (Class[]) null);
        method.invoke(device, (Object[]) null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

答案 2 :(得分:0)

1)你的描述很模糊。你有例外吗?它显示错误的结果吗?发生了什么事?

2)您需要从PictureBox获取图像并使用其Save method

答案 3 :(得分:0)

当Picturebox调整大小以填充表单时,它的Image属性似乎保持不变。

所以你需要做的是处理PictureBox.OnSizeChanged事件,然后使用以下代码来调整图像大小:

private void pictureBox1_SizeChanged(object sender, EventArgs e)
{
    if ((pictureBox1.Image != null))
    {
        pictureBox1.Image = new Bitmap(pictureBox1.Image, pictureBox1.Size);
    }
}

要保存图像,请使用:

pictureBox1.Image.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);

希望有所帮助!