根据指定区域内容生成图片并进行分享总结

需求及思路
  1. 需要把特定的页面区域生成图片
  2. 分享生成的图片
最终结论
  1. 可以根据html2canvas来生成特定的图片
  2. 普通浏览器中可以把生成的图片直接下载下来,最简单的就是通过a标签下载
  3. 微信中不允许直接下载,只能进行长按进行保存的操作
  4. 对保存的图片再进行转发分享
效果

这里只展示如何生成图片的,下载的就不做展示了

效果

具体实现

本次尝试是在react项目中进行的,所以下面展示的代码直接是JSX的语法了

  1. 安装html2canvas依赖
// yarn add html2canvas
  1. 页面使用代码
import { Button, Image } from 'antd-mobile';
import styles from './index.less';
import { useCallback, useState } from 'react';
import ShareContainer from '@/components/ShareContainer';
// ShareContainer 是为了方便使用,创建的一个组件,大小依据外层容器的大小
// 生成的图片范围也为它的直接父级所覆盖的区域-本例中为蓝色线框的区域
export default function IndexPage() {
  const [create, setCreate] = useState<any>();
  const onClick = useCallback(() => {
    create();
  }, [create]);
  return (
    <div className={styles.layout}>
      <ShareContainer setCreate={(create) => setCreate(create)}>
        <div className={styles.content}>
          <div className={styles.top}></div>
          <div className={styles.btn}>
            <Button color="primary" block onClick={onClick}>
              click
            </Button>
          </div>
          <div className={styles.bottom}>
            <Image src='/123.png' />
          </div>
        </div>
      </ShareContainer>
    </div>
  );
}

样式文件

.layout {
  width: 90%;
  height: 90%;
  border: 1px solid blue;
}
.content {
  width: 100%;
  height: 100%;
  .top {
    width: 100%;
    height: 128px;
    background: url(/avatar.png);
    background-size: 64px 64px;
  }
  .btn {
    width: 100%;
    margin: 8px 0;
  }
  .bottom {
    width: 100%;
  }
}
  1. ShareContainer组件代码
import { ImageViewer } from 'antd-mobile';
import styles from './index.less';
import html2canvas from 'html2canvas';
import { useCallback, useEffect, useRef, useState } from 'react';

interface Props {
  children: any;
  setCreate?: (create: any) => void;
}

export default function IndexPage({ children, setCreate }: Props) {
  const [url, setUrl] = useState('');
  const [visible, setVisible] = useState(false);
  const boxRef = useRef<any>();
  const onCreate = useCallback(() => {
    if (boxRef.current) {
      html2canvas(boxRef?.current, { scale: 1 }).then(function (canvas) {
        const aurl = canvas.toDataURL('image/png');
        canvas.getContext('2d')?.save();
        setUrl(aurl);
        setVisible(true);
      });
    }
  }, [url]);
  useEffect(() => {
    if (setCreate) {
      setCreate(() => onCreate);
    }
  }, [setCreate]);
  return (
    <div className={styles.box + ' share-container'} ref={boxRef}>
      <ImageViewer
        image={url}
        visible={visible}
        onClose={() => {
          setVisible(false);
        }}
        renderFooter={() => <div className={styles.tip}>长按保存图片</div>}
      />
      {children}
    </div>
  );
}
// 如果需要在普通浏览器中进行下载。这里可以把 长按保存图片 换成<a download href={url}></a>标签
// 这样可以直接利用a标签进行下载生成的图片

样式文件

.box {
  width: 100%;
  height: 100%;
}
.tip {
  width: 100%;
  text-align: center;
  font-size: 18px;
  line-height: 60px;
  color: #fff;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Python截取Excel指定区域生成图片,首先需要安装openpyxl和PIL库,openpyxl用于处理Excel文件,PIL用于生成图片。 首先,我们需要打开Excel文件,然后选择指定区域。可以通过openpyxl库中的load_workbook函数进行打开,并使用active属性或者获取sheet之后使用cell函数来定位需要截取的区域。 接下来,可以使用PIL库中的Image模块来创建一个新的图片对象。然后,通过遍历Excel指定区域内容,并将其添加到图片对象中相应的位置。 最后,可以使用PIL库的save函数将图片保存到本地磁盘。 整个过程的代码可以分为以下几个步骤: 1. 导入openpyxl和PIL库 2. 打开Excel文件 3. 选择指定区域 4. 创建一个新的图片对象 5. 将Excel指定区域内容添加到图片对象中 6. 保存图片到本地磁盘 一个简单的示例代码如下: ```python import openpyxl from PIL import Image, ImageDraw # 打开Excel文件 wb = openpyxl.load_workbook('example.xlsx') sheet = wb.active # 选择指定区域 cell_range = sheet['A1:C3'] # 创建一个新的图片对象 img = Image.new('RGB', (300, 300), color = (255, 255, 255)) draw = ImageDraw.Draw(img) # 将Excel指定区域内容添加到图片对象中 for row in cell_range: for cell in row: draw.text((cell.column * 100, cell.row * 100), cell.value, fill=(0, 0, 0)) # 保存图片到本地磁盘 img.save('output.png') ``` 这样就可以使用Python截取Excel指定区域生成图片了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值