Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom `PNG` image to material ui icon

I am using material-ui@next for my react app. I want to use Icon component with dynamic images in png format. I googled this but cannot find anything directly helpful.

like image 349
Nayan Srivastava Avatar asked Apr 25 '18 09:04

Nayan Srivastava


People also ask

How to create a custom IMG component with material-UI?

There is no such specific custom img component avaiable with material-ui. But you can use the simple html img component inside another wrapper components to create custom img component by your own. e.g Also <CardMedia/> component has to be used with in conjunction with <Card/> component.

How do I add material icons to my module’s imports?

And add it to the module’s array of imports: Now, you can use the built-in material icons with the <mat-icon> component. If you add the textual name for an icon, it will display the associated icon glyph. For our tutorial, we will be adding the "mood" icon (this resembles the symbol of a face with a smile):

How to add a custom Unicorn icon to the component tag?

To use our custom unicorn icon with the <mat-icon> component tag, we’ll need to import HttpClientModule. And add it to the module’s array of imports: Now, we can register our custom "unicorn" icon with the MatIconRegistry service provided by Angular Material. And add the injection of the service into the component:

How to use SVG icons in angular material components?

Let’s learn how to use our own SVG icons in Angular Material components. In this tutorial, you will use the <mat-icon> component to use the standard Material Icons font. Then, you will use the <mat-icon> component to support a custom SVG icon. The full working code can be found on this GitHub repo.


2 Answers

You can saveas PNG images to BMP format then convet them to SVG with potrace app.

potrace icon1.bmp -s -o icon1.svg

Now you can use SvgIcon component. open svg file with notepad and copy tags that are in svg tag and put it into SvgIcon:

function Icon1(props) {
return (
<SvgIcon {...props} >
// tags in your svg file
// ex: <path d="M81 1032 c-19 -9 "/>
</SvgIcon>
);
}

Your icon component is ready:

<Icon1 /> 
like image 145
karim fereidooni Avatar answered Sep 19 '22 03:09

karim fereidooni


You can:

  • use: https://convertio.co/png-svg/ to convert your image to svg format
  • download the file
  • open the .svg file in a text editor

In your code:

import * as React from "react";
import { SvgIcon } from "@material-ui/core";

export default function Icon1(props) {
  return (
    <SvgIcon
      {...props}
      ... // add here the params that are sent to <svg /> tag in your file 
   /*
    mine were something like:
      version="1.0"
      xmlns="http://www.w3.org/2000/svg"
      width="3000.000000pt"
      height="1068.000000pt"
      viewBox="0 0 3000.000000 1068.000000"
      preserveAspectRatio="xMidYMid meet">
    */
    >
    // The tags inside the <svg />; probably something like <g/> <path/> tags
    </SvgIcon>
  );
}

like image 45
Ashar Dweedar Avatar answered Sep 20 '22 03:09

Ashar Dweedar