- Home /
Load only a portion of the image into texture
I'm trying to load an image to my rawimage. It's already loading but it looks compressed bc it's showing the whole image. I want it to load only the right most part of the image in the texture. This is my code. Thanks in advance.
string url = Application.persistentDataPath +"/" + _CaptureCounter.ToString();
byte [] bytes = File.ReadAllBytes(url);
Texture2D texture = new Texture2D(1, 1, TextureFormat.RGB24, false);
texture.LoadImage(bytes);
ImagePrev.texture= texture ;
Answer by techmage · May 19, 2016 at 06:14 AM
You will probably want to use:
public void SetPixels(int x, int y, int blockWidth, int blockHeight, Color[] colors, int miplevel = 0);
http://docs.unity3d.com/ScriptReference/Texture2D.SetPixels.html
It works on Texture2D.
Although I notice now if your only wanting the rightmost part of the texture your loading in you will actually want get pixels with the blockWidth and blockHeight.
http://docs.unity3d.com/ScriptReference/Texture2D.GetPixels.html
Take your Texture2D texture in your examples above. Then do .GetPixels passing in the x,y, blockWidth and BlockHeight of the section of pixels you want. Then create a new Texture2D and SetPixels those pixels into that.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081109im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
cant get Texture2D.LoadImage() to work 0 Answers
How to load with Texture2D.LoadImage? -1 Answers
iOS crashing on Texture2D LoadImage 1 Answer